c++11 array initialization won't call copy constructor

倾然丶 夕夏残阳落幕 提交于 2019-11-28 11:43:14

You've encountered a defect in C++: list-initialization from a single element. The behaviour specified in the C++11 and C++14 International Standard is surprising. I'll refer to C++14 below.

Template instantiations of std::array are aggregates [array.overview]/2. Therefore, when initializing std::array objects from a braced-init-list, aggregate-initialization will be performed indiscriminately of the number of initializers [dcl.init.list]/3.1. Other container classes cannot be aggregates because of the requirements for certain constructions (e.g. from a pair of iterators).

Aggregate-initialization initializes (potentially recursively) the data members from the initializers. In your case, it will try to initialize the first data member of std::array<sf::Keyboard::Key, N> from the initializer sequence (which is of the same type). For all implementations of std::array I know, the first data member of std::array is a C-style array. List-initialization will then try to initialize the first element of that array from the original initializer: sequence.

Example:

struct aggregate
{
    int m[2];
};

aggregate x = {0, 1};
assert(x.m[0] == 0 && x.m[1] == 1);

aggregate y{x}; // error: cannot convert `aggregate` to `int`

The initialization in the last line will try to initialize y.m[0] from x.


CWG issue 1467 describes this and a related issue, list-initializing when there are no initializers. The proposed resolution introduces a (yet another) special case for list-initialization that covers the issue in the OP. Quoting a recent github draft, [dcl.init.list]/3.1

If T is a class type and the initializer list has a single element of type cv U, where U is T or a class derived from T, the object is initialized from that element (by copy-initialization for copy-list-initialization, or by direct-initialization for direct-list-initialization).

Aggregate-initialization in recent drafts has lower "priority" (3.3), that is, will only be performed if the condition above is not met.


Recent versions of g++ (5.0) and clang++ (3.7.0) implement the proposed resolution even in C++11 mode.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!