14

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, i.e., 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.

PeterSom
  • 1,939
  • 16
  • 16

4 Answers4

3

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.

ecatmur
  • 137,771
  • 23
  • 263
  • 343
3

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.

Community
  • 1
  • 1
emsr
  • 13,551
  • 6
  • 45
  • 59
2

A defaulted 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".

user541686
  • 189,354
  • 112
  • 476
  • 821
1

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
juanchopanza
  • 210,243
  • 27
  • 363
  • 452