The case where a copy constructor is implicitly defined as remote

Section N3797::12.8/11 [class.copy] says:

An implicitly declared copy / move constructor is an inline public member of its class. The default copy / move constructor for class X is defined as remote (8.4.3) if X has:

[...]

- non-static data is a member of the class M (or its array) that cannot be copied / moved since the overload resolution (13.3) with respect to Ms corresponds to the constructor, leads to ambiguity or a function that is removed or inaccessible from the constructor with default

The first case of the ambiguity of the corresponding copy / move constructor is clear enough. We can write the following:

 #include <iostream> using namespace std; struct A { A(){ } A(volatile A&){ } A(const A&, int a = 6){ } }; struct U { U(){ }; A a; }; U u; U t = u; int main(){ } 

to reflect this. But what about a function that was deleted or inaccessible from the default constructor? What happened with a function inaccessible from the default constructor? Could you give an example reflecting this?

+7
c ++ constructor
source share
2 answers

Simply put:

 struct M { M(M const&) =delete; }; struct X { X(X const&) =default; M m; }; // X(X const&) is actually deleted! 

Implicitly declared functions are also considered "default" ([dcl.fct.def.default] / 5); a more familiar example of pre-C ++ 11 might be something like:

 struct M { protected: M(M const&); }; struct X { M m; }; // X implicit copy constructor is deleted! 

Please note that if you explicitly set a function after it has been declared, the program will be poorly formed if the function is implicitly deleted ([dcl.fct.def.default] / 5):

 struct M { M(M const&) =delete; }; struct X { X(X const&); M m; }; X::X(X const&) =default; // Not allowed. 
+6
source share

a non-static data member of class M (or its array) that cannot be copied / moved because the overload resolution (13.3), applicable to the corresponding constructor of Ms, leads to ambiguity or a function that is removed or inaccessible from the default constructor

The wording is perhaps a little far-fetched, for brevity, of course. The idea, as emphasized above, is that the function in question copy constructor M overloaded in a way that makes it inaccessible. Therefore, having a member of class M whose constructor is protected , for example, delete the copy constructor X Similarly, simply removing the copy constructor M will have the same result.

0
source share

All Articles