How to initialize array of classes with deleted copy constructor (C++11) How to initialize array of classes with deleted copy constructor (C++11) arrays arrays

How to initialize array of classes with deleted copy constructor (C++11)


I agree with the comments that this seems to be a GCC bug (reported as 63707).

It only fails to compile when the type in the array has a user-defined destructor, which doesn't make sense to me.


I came through a similar issue, namely that this code

#include <iostream>class Widget {public:    Widget(int i) { std::cout << "Ctor " << i << std::endl; }    Widget(const Widget&); // = delete;};int main() {    Widget w = 123;}

compiled and gave the expected result, but after uncommenting the = delete it failed to compile with gcc-4.9.

After reading the standard I believe the answer lies in the second item of highest indentation in 8.5/16, which is cited below.

What basically seems to happen is that the compiler conceptually wants to create a temporary of type Widget and direct-initialize the actual object w from that temporary through the copy constructor. Since the copy constructor is deleted, compilation stops. If the copy constructor was not deleted, the compiler would later realize that it may elide the copy, but it does not get that far.

Here is the relevant part:

[...] for the [...] copy-initialization cases [...] user-defined conversion sequences that can convert from the source type to the destination type [...] are enumerated as described in 13.3.1.4, and the best one is chosen through overload resolution (13.3). [...] The function selected is called with the initializer expression as its argument; if the function is a constructor, the call initializes a temporary of the cv-unqualified version of the destination type. [...] The result of the call (which is the temporary for the constructor case) is then used to direct-initialize, according to the rules above, the object that is the destination of the copy-initialization. In certain cases, an implementation is permitted to eliminate the copying inherent in this direct-initialization by constructing the intermediate result directly into the object being initialized; see 12.2, 12.8.

But I may be wrong since there is a lot in the [...] parts that I did not understand.