Is the default move constructor reviewed by the user?

The question is how my headline claims.

I ask because I have a class with a default move constructor, but the code trying to perform copy assignment does not indicate that the copy assignment operator has been deleted (according to Visual Studio 2015).

So, I checked the rules here for implicitly declared copy assignment operators:

An assignment operator by implicit declaration or default for a class T is defined as deleted in any of the following statements:

  • ...
  • T has a declared move constructor
  • T has a custom move destination operator

So basically I'm not sure if the default move constructor is considered a declared user. My gut tells me yes, but when it comes to the standard, I always like to be sure, because assumptions can be costly.

+7
c ++ c ++ 11
source share
2 answers

The standard says:

12.8 Copying and moving class objects [class.copy]

If the class definition does not explicitly declare the copy constructor, it is declared implicitly. If the definition class declares a move constructor or a move assignment operator, the implicitly declared copy constructor is defined as deleted; otherwise, it is defined as default (8.4). The latter case is deprecated if the class is a user-declared copy assignment operator or a user-declared destructor.

If the class definition does not explicitly declare the copy assignment operator, one is declared implicitly. If a class definition declares a move constructor or moves an assignment statement, an implicitly declared copy of the assignment statement is defined as deleted; otherwise, it is defined as default (8.4). The latter case is deprecated if the class has a user-declared copy constructor or a user-declared destructor.

Your class has a default move constructor installed, but it is explicitly declared. Therefore, according to the standard implicit declaration, the copy constructor and copy assignment operator are defined as deleted.

8.4.2. Explicit Default Functions [dcl.fct.def.default]

Explicit default functions and implicitly declared functions are collectively called default functions, and the implementation must contain implicit definitions for them (12.1, 12.4, 12.8), which may mean their removal. A function is provided by the user if it is declared by the user and is clearly not defaulted or deleted by its first declaration. The user-provided function of an obviously default value (that is, an explicit default by default after its first declaration) is defined in the place where it is clearly defaulted.

Using this terminology, your move constructor is declared by the user, but not provided by the user.

+1
source share

A special member function is declared by default by the user, but it is also defined by the user as default. The standard does not explicitly define the term "user-declared", but essentially means any special member function that must be written by the user. Thus, the following declares the constructor and defines it as default.

struct X { X() = default; // declaration and definition }; 

Defining a member function by default means that the definition is equivalent to an implicit definition. It is declared by the user due to the fact that it must be printed by the user.

0
source share

All Articles