In C ++ 11, you can explicitly specify a special member function if its implicit generation was automatically prevented.
However, explicitly prohibiting the execution of a special member function cancels the implicit deletion caused by the manual declaration of some other special member functions (copy operations, destructor, etc.), this does not force the compiler to generate the function and the code is considered well-formed, even if the function actually cannot be generated.
Consider the following scenario:
struct A { A () = default; A (const A&) = default; A (A&&) = delete; // Move constructor is deleted here }; struct B { B () = default; B (const B&) = default; B (B&&) = default; // Move constructor is defaulted here A a; };
The move constructor in B will not be generated by the compiler, because this will lead to a compilation error (the move constructor is deleted). Without explicitly deleting constructor B, the move constructor B will be generated as expected (copying A, not moving it).
Attempting to move such an object will silently use the copy constructor instead:
B b; B b2 (std::move(b));
Is there a way to force the compiler to either generate a function or throw a compilation error if it cannot? Without this guarantee, it is difficult to rely on default move constructors if one remote designer can turn off move for entire object hierarchies.
source share