c++ - 如何使用 static const 变量初始化 static std::array ?

标签 c++ c++11 stdarray

如何初始化使用 static const 变量作为大小的 static std:array?我尝试搜索类似的问题,但 std::array 相对较新,因此找不到太多内容。

// ExampleClass.h
class ExampleClass {
    public:
        static const size_t NUMBER_OF_INDEXES = 5;
    private:
        static std::array<int, NUMBER_OF_INDEXES> myArray;
};

最佳答案

与任何其他静态数据成员一样,ExampleClass::myArray 应该在一个翻译单元中具有行外定义,您可以像往常一样记下其类型及其限定名称:

std::array<int, ExampleClass::NUMBER_OF_INDEXES> ExampleClass::myArray = {1, 2, 3, 4, 5};

关于c++ - 如何使用 static const 变量初始化 static std::array ?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53271457/

相关文章:

c++ - 在 std::getline() 上使用任何字符作为分隔符

c++ - 模棱两可的调用 : int to double or bool

c++ - 小于 X 的元素数

c++ - 在取消引用的对象指针上调用成员函数

c++ - 没有返回,在函数中返回非空 {在 eclipse CDT 中}

c++ - 如果我将它绑定(bind)到 "this", std::bind 是否保留对象的引用/拷贝?

c++ - 使用 std::array 作为 boost::spirit::x3 的属性

c++ - 我如何在我的矩阵类中获得此功能?

c++ - 如何初始化 std::array<std::atomic<bool>> — 没有复制或移动 ctors

c++ - 当 std 数组初始化太小时,C++ 会引发错误吗?