std::array的内联初始化有什么问题?

时间:2021-08-26 18:29:27

Consider the following declaration:

考虑以下声明:

#include <array>

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

As is, it compiles normally by g++ 5.2. But if I uncomment the std::array, I get an error:

实际上,它通常是通过g++ 5.2进行编译。但是如果我取消对std::数组的注释,我会得到一个错误:

test.cpp:5:46: error: array must be initialized with a brace-enclosed initializer
     std::array<bool,3> arr={false,false,false};
                                              ^
test.cpp:5:46: error: too many initializers for ‘std::array<bool, 3u>’

OTOH, this declaration works without problems inside main(). Also, the following initialization does work inside struct X:

OTOH,这个声明在main()内部没有问题。另外,下面的初始化在struct X中工作:

std::array<bool,3> arr={{false,false,false}};

Why can't I use the simple initialization with single braces in struct definition?

为什么我不能在struct定义中使用带有单个大括号的简单初始化呢?

1 个解决方案

#1


22  

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

这看起来像一个gcc bug看:bug 65815 -在NSDMI中不起作用。报告说:

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

在《c++编程语言》第4版的975页,Bjarne Stroustrup说:

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

“可以通过初始化器列表初始化数组:数组a1 ={1,2,3};”

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

Clang (v3.5)接受了它。然而,g++ 4.9.2认为这是一个错误:

"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.

看起来修正在head修订中,操作代码在那个修订中工作,看它如何运行。

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

正如bug报告中所指出的,使用内嵌套的大括号是一种可能的解决方法:

std::array<bool,3> arr={ {false,false,false} };
                         ^                 ^

#1


22  

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

这看起来像一个gcc bug看:bug 65815 -在NSDMI中不起作用。报告说:

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

在《c++编程语言》第4版的975页,Bjarne Stroustrup说:

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

“可以通过初始化器列表初始化数组:数组a1 ={1,2,3};”

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

Clang (v3.5)接受了它。然而,g++ 4.9.2认为这是一个错误:

"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.

看起来修正在head修订中,操作代码在那个修订中工作,看它如何运行。

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

正如bug报告中所指出的,使用内嵌套的大括号是一种可能的解决方法:

std::array<bool,3> arr={ {false,false,false} };
                         ^                 ^