英文:
What is the order of destruction of the two entries of a std::pair?
问题
只允许使用5个标签,但请将[tag:c++20]和[tag:c++23]也视为在列表中,因为我想了解这些标准,以防自[tag:c++17]以来发生了变化。
两个std::pair
条目的销毁顺序(而不是std::tuple
或其他容器)是否由标准规定?在标准草案中的哪个部分可以找到这些信息?
此外,如果确实指定了顺序,那么从何时开始?
在我的GCC实现中,我看到这样的代码:
namespace std {
// ...
template<typename _T1, typename _T2>
struct pair
: private __pair_base<_T1, _T2>
{
typedef _T1 first_type; /// The type of the `first` member
typedef _T2 second_type; /// The type of the `second` member
_T1 first; /// The first member
_T2 second; /// The second member
// ...
所以很明显second
在first
之前销毁,但这只是GCC的选择还是标准要求?
实际上,在标准草案中快速搜索会发现,§22.3.2显示了std::pair
类的外观,它开始如下:
namespace std {
template<class T1, class T2>
struct pair {
using first_type = T1;
using second_type = T2;
T1 first;
T2 second;
这只是一个示例吗?还是一个规定?
英文:
Only 5 tags are allowed, but please, take it as [tag:c++20] and [tag:c++23] are in the list too, because I'd like to know about those standards as well, in case something has changed since [tag:c++17].
Is the order of destruction of the two entries of a std::pair
(not std::tuple
, nor other containers) specified by the standard? Where in the standard draft can I find this information?
Also, in case the order is indeed specified, since when has it been?
In my GCC implementation I see this
namespace std {
// ...
template<typename _T1, typename _T2>
struct pair
: private __pair_base<_T1, _T2>
{
typedef _T1 first_type; ///< The type of the `first` member
typedef _T2 second_type; ///< The type of the `second` member
_T1 first; ///< The first member
_T2 second; ///< The second member
// ...
so it's clear that second
is destroyed before first
, but is it just a choice of GCC or is it mandated by the standard?
Actually, a quick search in the draft reveals that §22.3.2 shows what the std::pair
class looks like, and it starts like this:
namespace std {
template<class T1, class T2>
struct pair {
using first_type = T1;
using second_type = T2;
T1 first;
T2 second;
Is it just an example? Or is it a mandate?
答案1
得分: 4
订单已在标准中指定:
template<class T1, class T2>
struct pair {
using first_type = T1;
using second_type = T2;
T1 first;
T2 second;
//...
}
成员的定义未标记为“仅供阐述”,因此它们是强制性的。
英文:
The order is specified in the Standard:
template<class T1, class T2>
struct pair {
using first_type = T1;
using second_type = T2;
T1 first;
T2 second;
//...
}
The definitions of the members are not marked as "exposition only", hence, they are mandatory.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论