Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is a defaulted move constructor considered user-declared?

Tags:

c++

c++11

The question is as my title states.

I'm asking because I have a class with a defaulted move constructor but code trying to perform copy assignment is failing stating that the copy assignment operator is deleted (according to Visual Studio 2015).

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

The implicitly-declared or defaulted copy assignment operator for class T is defined as deleted in any of the following is true:

  • ...
  • T has a user-declared move constructor
  • T has a user-declared move assignment operator

So basically I'm not sure if a defaulted move constructor counts as user-declared. My gut tells me yes but when it comes to standardese I always like to be sure since assumptions can be costly.

like image 615
void.pointer Avatar asked Aug 06 '15 21:08

void.pointer


1 Answers

The standard says:

12.8 Copying and moving class objects [class.copy]

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

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

Your class has defaulted move constructor, but it is explicitly declared. So according to standard implicitly declared copy constructor and copy assignment operator is defined as deleted.

8.4.2 Explicitly-defaulted functions [dcl.fct.def.default]

Explicitly-defaulted functions and implicitly-declared functions are collectively called defaulted functions, and the implementation shall provide implicit definitions for them (12.1 12.4, 12.8), which might mean defining them as deleted. A function is user-provided if it is user-declared and not explicitly defaulted or deleted on its first declaration. A user-provided explicitly-defaulted function (i.e., explicitly defaulted after its first declaration) is defined at the point where it is explicitly defaulted.

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

like image 161
Elohim Meth Avatar answered Nov 15 '22 11:11

Elohim Meth