If class X is derived from class Y and class Y has any of:
- A user-declared copy constructor,
- A user-declared copy assignment operator,
- A user-declared destructor
- A user-declared move constructor,
- A user-declared move assignment operator,
Will a move constructor and move assignment operator be implicitly defaulted for class X providing it declares none of the above?
e.g.
struct Y
{
virtual ~Y() {}
// .... stuff
};
struct X : public Y
{
// ... stuff but no destructor,
// no copy/move assignment operator
// no copy/move constructor
// will X have a default move constructor / assignment operator?
};
I am currently using gcc, but I am mainly interested in what the correct behaviour should be (as opposed to whether or not a particular compiler is standards compliant)..
As far as the derived class is concerned, the fact that an attribute or a base class has user-defined or implicit special functions does not matter. All that matter is their presence.
Compliant C++11 compilers should automatically provide the move constructors and assignment operators for the struct and class, if possible (which is clearly defined in the Standard), even though only classes with dynamically allocated buffers will really benefit from it (moving an
intis just copying it).Therefore, if your class embeds a
std::stringor astd::unique_ptr(for example), then its move constructor will call the embeddedstringorunique_ptrmove constructor and it will be efficient… for free.Simply changing the compilation mode should thus slightly improve performance.