mardi 27 janvier 2015

Different behavior of c++11 list-initialization

Please consider the following code:



class A {
private:
std::string s;
public:
A() = delete;
A(const A&) = delete;
A(A&&) = delete;
A(const std::string &a) : s(a) {}
};


Now, I would like to initialize an array of A using list initialization. g++ (4.9.1) could successfully build the following code:



int main() {
A arr[2] = {{"a"}, {"b"}};
return 0;
}


But, it failed for the following code:



class Aggr {
private:
A arr[2];
public:
Aggr() : arr{{"a"}, {"b"}} {}
};


The error messages are,



test.cc: In constructor ‘Aggr::Aggr()’:
test.cc:22:28: error: use of deleted function ‘A::A(A&&)’
Aggr() : arr{{"a"}, {"b"}} {}
^
test.cc:11:3: note: declared here
A(A&&) = delete;
^


That said, a list-initializer tries to call a move constructor for initializing an array inside of a class. That code, however, was successfully built by clang v3.5 without any warnings. So, I would like to know what the C++11 (or later version) specifies rules with respect to list-initialization. Thanks in advance.


Aucun commentaire:

Enregistrer un commentaire