如何初始化像 std::array 这样的 class

How to initialize a class like std::array

我正在尝试为向量运算编写一个容器 class。对象的大小是静态的:

template<typename T, unsigned N>
class vec{
    T data[N] = {0};
public:
    vec(std::initializer_list<T> ini){
        std::copy(ini.begin(), ini.end(), data);
    }
}

这就是我的进展。

但是我测试了 std::array class 进行比较,我注意到如果初始化列表太长或太短,它可以以某种方式进行静态断言。

std::array<float, 2> a = {1, 2, 3, 4} <- instant error message from the visual studio ide

在我的 class 中,我必须在 运行 时检查初始化列表的长度。

我假设 std::array class 以某种方式管理它以使用初始化列表符号直接初始化数据,而不使用 std::initializer_list class.

是否可以像 std::array 一样初始化我的 class?

std::array is an aggregate and so it uses aggregate initialization. Providing excess elements during aggregate initialization is ill-formed and requires a diagnostic. The compiler at minimum has to provide a warning, both gcc and clang make this an error. So if you make your class an aggregate then you can have it work the same way std::array does. Note, in class member initializers makes your class a non-aggregate 在 C++11 中,但在 C++14 中没有。

我们可以通过转到C++11标准草案部分看到它是一个聚合23.3.2.1 [array.overview]:

An array is an aggregate (8.5.1) that can be initialized with the syntax

array<T, N> 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 [dcl.init.aggr] 部分涵盖聚合初始化并说:

An initializer-list is ill-formed if the number of initializer-clauses exceeds the number of members or elements to initialize.

标准草案提供了一种可能的实施方式,它缩短到最低限度,如下所示:

template <class T, size_t N>
struct array {
    T elems[N];
};

并且标准草案有一条说明:

The member variable elems is shown for exposition only, to emphasize that array is a class aggregate. The name elems is not part of array’s interface

这是一个集合,如果提供了过多的元素,gcc 和 clang 都会报错。​​

另见 What are Aggregates and PODs and how/why are they special?