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.
A copy constructor is automatically invoked by the compiler whenever we pass or return an object by value. We can restrict an object passed or return by value using a private copy constructor. A default copy constructor is fine, but be cautious if there is a dynamic data member.
It's a new C++11 feature. It means that you want to use the compiler-generated version of that function, so you don't need to specify a body. You can also use = delete to specify that you don't want the compiler to generate that function automatically.
A default constructor is a constructor that either has no parameters, or if it has parameters, all the parameters have default values. If no user-defined constructor exists for a class A and one is needed, the compiler implicitly declares a default parameterless constructor A::A() .
The default constructor is auto-generated if there is no user-declared constructor (§12.1/5).
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With