Why does initialization of array of pairs still need double braces in C++14?

后端 未结 5 860
死守一世寂寞
死守一世寂寞 2021-01-31 13:44

With the C++14 standard, the initialization of an std::array can go with single braces (see http://en.cppreference.com/w/cpp/container/array):

This, however

5条回答
  •  执笔经年
    2021-01-31 14:10

    I'm going to guess here.
    The initializer list for std::array should be a list of T (or trivially constructable to T). So you could do

    std::array,3> b { std::pair{1,11}, std::pair{2,22}, std::pair{3,33} };
    

    but that is tediously verbose. In order to get the conversion to std::pair you want, you need to provide an initializer list, so

    std::array,3> b {
        { // element 1
          { // initialize from:
            { 1,11 } // std::initializer_list
           }
         },
      ...
    };
    

    I can't defend this further, but note that std::vector::vector( std::initializer_list, const Allocator& alloc=Allocator()) is defined but std::array::array( std::initializer_list ) is not. Neither is std::pair::pair( std::initializer_list ) defined.

提交回复
热议问题