I'm making a little class that uses an array templated on its size. Here's some code...
.hpp
template <size_t N>
class KeyCombinationListener
{
public:
KeyCombinationListener(
const std::array<sf::Keyboard::Key, N>& sequence,
std::function<void (void)> fn
);
private:
std::array<sf::Keyboard::Key, N> combo;
std::function<void (void)> callback;
};
.cc
template <size_t N>
KeyCombinationListener<N>::KeyCombinationListener(
const array<sf::Keyboard::Key, N>& sequence, function<void (void)> fn
) : combo(sequence), progress{begin(combo)}, callback{fn}
{
}
In the member initialization of the constructor, I can't use combo{sequence}
as the initializer because it only accepts sf::Keyboard::Key
types. This makes sense if it's asking for an initializer_list
, but this seems strange to me. With other standard containers I can call a copy constructor using {} notation just fine. Is this a quirk with std::array
? Or maybe a bug in my clang?
Just in case it helps, here's my clang version:
Debian clang version 3.5.0-10 (tags/RELEASE_350/final) (based on LLVM 3.5.0)
Target: x86_64-pc-linux-gnu
Thread model: posix
Found candidate GCC installation: /usr/bin/../lib/gcc/x86_64-linux-gnu/4.9
Found candidate GCC installation: /usr/bin/../lib/gcc/x86_64-linux-gnu/4.9.2
Found candidate GCC installation: /usr/lib/gcc/x86_64-linux-gnu/4.9
Found candidate GCC installation: /usr/lib/gcc/x86_64-linux-gnu/4.9.2
Selected GCC installation: /usr/bin/../lib/gcc/x86_64-linux-gnu/4.9
Candidate multilib: .;@m64
Selected multilib: .;@m64
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 cvU
, whereU
isT
or a class derived fromT
, 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.
来源:https://stackoverflow.com/questions/29440617/c11-array-initialization-wont-call-copy-constructor