What's wrong with this inline initialization of std::array?

前端 未结 1 2075
攒了一身酷
攒了一身酷 2020-12-07 01:08

Consider the following declaration:

#include 

struct X
{
    //std::array arr={false,false,false};
    bool brr[3]={false,false,f         


        
相关标签:
1条回答
  • 2020-12-07 01:44

    This looks like a gcc bug see: Bug 65815 - brace elision doesn't work in NSDMI. The report says:

    On Page 975 of "The C++ Programming Language", 4th edition, Bjarne Stroustrup says:

    "An array can be initialized by an initializer list: array a1 = { 1, 2, 3 };"

    and Clang (V 3.5) accepts it. However, G++ 4.9.2 thinks this is an error:

    "error: array must be initialized with a brace-enclosed initializer
       const std::array<double, 3> _ar0val = {1.0, -1.0, 1.0};"
    

    The issue was narrowed down to the following test case:

    struct array {
      int data [2];
    };
    
    struct X {
      array a = { 1, 2 };
    };
    

    It looks like the fix is in the head revision, the OPs code works in that revision, see it live.

    As noted in the bug report using an inner set of braces is a possible work-around:

    std::array<bool,3> arr={ {false,false,false} };
                             ^                 ^
    
    0 讨论(0)
提交回复
热议问题