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

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

    Without the double braces, the statement is simply ambiguous. Consider the following code:

        std::array, 1> a = {{ {1, 2} }};
        std::array b = { {1, 2} };
    

    Without double braces in the first definition, the compiler will treat { {1,2} } as a scalar initialization list for array. You need to declare an explicit nested braced-init-list in order for the compiler to recognize that the inner list is also aggregate-initialized (vs. scalar initialized), such that it can construct an array of std::pair.

提交回复
热议问题