user2953119
user2953119

Reputation:

The case when the copy-constructor implicitly defined as deleted

The section N3797::12.8/11 [class.copy] says:

An implicitly-declared copy/move constructor is an inline public member of its class. A defaulted copy/ move constructor for a class X is defined as deleted (8.4.3) if X has:

[...]

— a non-static data member of class type M (or array thereof) that cannot be copied/moved because overload resolution (13.3), as applied to M’s corresponding constructor, results in an ambiguity or a function that is deleted or inaccessible from the defaulted constructor

The first case about the ambiguity of corresponding copy/move constructor is quite clear. 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 that. But what about or a function that is deleted or inaccessible from the defaulted constructor? What's that got with a function inaccessible from the default constructor? Could you provide an example reflecting that?

Upvotes: 7

Views: 1339

Answers (2)

user657267
user657267

Reputation: 21000

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 "defaulted" ([dcl.fct.def.default] / 5); a more familiar pre-C++11 example might be something like:

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

Note that if you explicitly default the function after it has been declared, the program is ill-formed if the function would be 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.

Upvotes: 6

didierc
didierc

Reputation: 14730

a non-static data member of class type M (or array thereof) that cannot be copied/moved because overload resolution (13.3), as applied to M’s corresponding constructor, results in an ambiguity or a function that is deleted or inaccessible from the defaulted constructor

The wording is perhaps slightly contrived, for conciseness most certainly. The idea, as emphasised in the above, is that the function in question is the M copy constructor being overloaded in way which renders it inaccessible. So having a member of class M whose copy constructor is made protected for instance would delete the copy constructor of X. Likewise, simply deleting the copy constructor of M would have the same result.

Upvotes: 0

Related Questions