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

后端 未结 5 862
死守一世寂寞
死守一世寂寞 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:16

    The C++14 brace elision rule applies only to subaggregate initialization.

    So for example something like this works:

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

    Here an aggregate of aggregates can be list-initialized without extra braces.

    But std::pair is not an aggregate (it has constructors), so the rule does not apply.

    Which means that without the brace elision rule, std::array, itself being an aggregate with an array inside, needs an extra set of braces in order to be list-initialized. Remember that the class template array is implemented as:

    template 
    struct array {
      T elems[N];
    };
    

    To list-initialize it without the brace elision rule, you need an extra set of braces to get to the elems member.

提交回复
热议问题