Brace elision in std::array initialization

前端 未结 2 1697
夕颜
夕颜 2020-12-01 11:53

Suppose there\'s an std::array to be initialized. It\'s okay if using double braces:

std::array x = {{0, 1}};
std::array

        
相关标签:
2条回答
  • 2020-12-01 12:17

    Relevant: http://en.cppreference.com/w/cpp/language/aggregate_initialization

    In short,

    struct S {
        int x;
        struct Foo {
            int i;
            int j;
            int a[3];
        } b;
    };
    S s1 = { 1, { 2, 3, {4, 5, 6} } };
    S s2 = { 1, 2, 3, 4, 5, 6}; // same, but with brace elision
    S s3{1, {2, 3, {4, 5, 6} } }; // same, using direct-list-initialization syntax
    S s4{1, 2, 3, 4, 5, 6}; // error in C++11: brace-elision only allowed with equals sign
                            // okay in C++14
    
    0 讨论(0)
  • 2020-12-01 12:36

    Brace elision applies, but not in C++11. In C++14, they will apply because of http://www.open-std.org/jtc1/sc22/wg21/docs/cwg_defects.html#1270 . If you are lucky, Clang will backport that to their C++11 mode (let's hope they will!).

    0 讨论(0)
提交回复
热议问题