声明一个静态私有数组成员

Declaring a static private array member

我的 foo class 需要一个静态 C++ array 作为我最终以这种方式声明的私有成员 :

class Foo : public Bar {

private:

    constexpr static array<int, 18> rouges = {1, 3, 5, 7, 9, 12, 14, 16, 18, 19, 21, 23, 25, 27, 30, 32, 34, 36};
    // ...
}

但是编译器抛出

error: array must be initialized with a brace-enclosed initializer
error: too many initializers for 'const std::array<int, 18u>'

有趣的是,我的数组大小恰好是 18 个元素,如果我声明它 array<int, 500>,我仍然会收到 too many initializers 错误。至于大括号初始化错误,我不明白编译器期望读取什么。

我通过查看 Stroustrup 的 C++ 之旅 (11.3.1 array) 来记录自己,但我看不出他的做法与我有何不同.或者,删除 constexpr static 关键字并不能消除错误。

感谢您的见解。

多戴一副牙套

constexpr static array<int, 18> rouges = { { 1, 3, 5, 7, 9, 12, 14, 16, 18, 19, 21, 23, 25, 27, 30, 32, 34, 36 } };
    // ...

实际上有两个集合,其中一个包含在另一个集合中。

std;:array 的初始化定义如下(23.3.2.1 Class 模板数组概述)

2 An array is an aggregate (8.5.1) that can be initialized with the syntax array a = { initializer-list }; where initializer-list is a comma-separated list of up to N elements whose types are convertible to T.

和(8.5.1 聚合)

11 Braces can be elided in an initializer-list as follows. If the initializer-list begins with a left brace, then the succeeding comma-separated list of initializer-clauses initializes the members of a subaggregate; it is erroneous for there to be more initializer-clauses than members.