简体   繁体   中英

C++ 11: is a defaulted copy constructor user declared?

I guess it is so, but I am looking for C++11 language lawyers to confirm my impression. Is it true that the following class

struct X{
X(){}
X(X const&)=default;
};

will not be automatically move-enabled, ie, getting X(X&&) and operator=(X&&) , because its copy constructor is "user-declared", even though it looks equivalent to

struct X{
};

which will get both X(X const&) and X(X&&) etc., implicitely declared and (trivially) defined on use.

From the standard:

8.4.2 Explicitly-defaulted functions [dcl.fct.def.default]

4 - [...] A special member function is user-provided if it is user-declared and not explicitly defaulted or deleted on its first declaration. [...]

An explicit default can be combined with its declaration, or it can be separate:

struct S {
    S();
};
S::S() = default;

In either case its (first) declaration makes it user-declared.

A default ed copy constructor is indeed "user-declared"; I think the addition of default was in fact the reason why they changed the term from "user-defined" to "user-declared".

Yes, your defaulted copy assign operator precludes the implicit move ctor.

BTW putting =default is actually a definition . I remember trying to implement a pimpl idiom with std::unique_ptr and having to remove =default from headers and putting them in the implementation file because the destructor for unique_ptr needed the definition of the class it is trying to clean up.

That is correct, §12.8 sets the conditions when a move constructor gets implicitly declared and the presence of a user-declared copy constructor precludes that. You cannot have

  • user-declared copy constructor
  • user-declared copy assignment operator
  • user-declared move-assignment operator
  • user-declared destructor

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM