Confused by default constructor description of std::tuple in the ISO C++ Standard

后端 未结 3 1713
孤独总比滥情好
孤独总比滥情好 2021-01-17 08:38

The Standard says that std::tuple has the following member functions

constexpr tuple();
explicit tuple(const Types&...);

C

3条回答
  •  隐瞒了意图╮
    2021-01-17 09:05

    I guess the definition given in the standard is supposed to be pseudocode. That is the case with many of the definitions in the standard; it contains several requirements that are given verbally, but are satisfiable only with tricks like enable_if. This seems to be an example where the C++-like pseudocode notation can actually lead to illegal C++ when trying to instantiate such an empty tuple (or it might just be an omission).

    Both stdlibc++ and libc++ have an explicit specialization for the zero-element tuple. For example, in stdlibc++:

      // Explicit specialization, zero-element tuple.
      template<>  
        class tuple<>
        {
        public:
          void swap(tuple&) noexcept { /* no-op */ }
        };
    

    with an implicitly-defined unambiguous default constructor.

    Libc++ does not explicitly declare the parameterless default constructor. Presumably the templated constructor is then chosen as default constructor for non-empty tuples.

    Interestingly, the two libraries disagree on what members the empty tuple has. For example, the following compiles with libc++, but not with libstdc++:

    #include 
    #include 
    
    int main() {
      std::tuple<> t(std::allocator_arg, std::allocator());
    }
    

提交回复
热议问题