Mr.C64 Mr.C64 - 1 year ago 86
C++ Question

Does forbidding copy operations automatically forbid move operations?

I want to write a C++ class without any copy and move semantics: I'm just interested in its constructor and destructor.

I disabled copy operations (i.e. copy constructor and copy assignment operator) explicitly using C++11's

syntax, e.g.:

class MyClass
MyClass() { /* Init something */ }
~MyClass() { /* Cleanup something */ }

// Disable copy
MyClass(const MyClass&) = delete;
MyClass& operator=(const MyClass&) = delete;

As a test, I tried calling
on class instances, and it seems that there are no move operations automatically generated, as the Visual Studio 2015 C++ compiler emits error messages.

Is this a behavior specific to MSVC 2015, or is it dictated by the C++ standard that disabling via
copy operations automatically disables move constructor and move assignment?

Answer Source

MSVC conforms to the standard in this case. [class.copy]/9 in C++14 reads:

If the definition of a class X does not explicitly declare a move constructor, one will be implicitly declared as defaulted if and only if

  • X does not have a user-declared copy constructor,
  • X does not have a user-declared copy assignment operator,
  • X does not have a user-declared move assignment operator, and
  • X does not have a user-declared destructor.

So your class has no move constructor and any attempt to move it will fall back to the deleted copy constructor.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download