std::pair的两个条目的销毁顺序是什么?

huangapple go评论96阅读模式
英文:

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
// ...

所以很明显secondfirst之前销毁,但这只是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&lt;typename _T1, typename _T2&gt;
    struct pair
    : private __pair_base&lt;_T1, _T2&gt;
    {
      typedef _T1 first_type;    ///&lt; The type of the `first` member
      typedef _T2 second_type;   ///&lt; The type of the `second` member

      _T1 first;                 ///&lt; The first member
      _T2 second;                ///&lt; 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&lt;class T1, class T2&gt;
  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&lt;class T1, class T2&gt;
  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.

huangapple
  • 本文由 发表于 2023年7月27日 18:40:15
  • 转载请务必保留本文链接:https://go.coder-hub.com/76778925.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定