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

前端 未结 1 1767
半阙折子戏
半阙折子戏 2020-12-10 20:38

I\'m making a little class that uses an array templated on its size. Here\'s some code...

.hpp

template 
class KeyCombinationListener         


        
相关标签:
1条回答
  • 2020-12-10 21:44

    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.

    0 讨论(0)
提交回复
热议问题