Special member functions in C ++ 0x

The Wikipedia article on special member functions does not contain references to moving constructors and transferring assignment operators.

I would like to update the record, but I'm not sure what the standard 0x says.

What are the rules regarding these two functions? They are automatically generated by the compiler, and if so, when?


Edit: I updated the page on Wikipedia, if someone likes this, please help the community by editing it in the form (if necessary).

+3
source share
1 answer

Bearing in mind that C ++ 0x is not quite a standard yet, this is subject to change. From the FCD (PDF link), moving constructors and transferring assignment statements can indeed be explicitly defaulted and even implicitly defaulted. *****


I'm just going to quote (in abbreviated form) a bunch of things that might be useful to take a look at:

In relation to explicitly defined functions, §8.4.2 / 1-2:

The function explicitly set by default should

  • - special member function,
  • have the same declared function type as if it were implicitly declared,
  • have no default arguments and
  • do not have specification exceptions.

If the first ad explicitly indicates a default value,

  • It must be publicly accessible.
  • it should not be explicit
  • it does not have to be virtual,
  • it is implied that it has the same exception specification as if it were implicitly declared (15.4) and
  • in the case of a copy constructor, constructor move, copy assignment operator, or redirection operator, it must have the same parameter type as if it were implicitly declared.

On special member functions, §12 / 1:

The default constructor (12.1), copy constructor and copy assignment operator (12.8), move the constructor and move assignment operator (12.8), and the destructor (12.4) - special member functions. [Note: the implementation implicitly declares these member functions for some types of classes when the program does not explicitly declare them. The implementation will implicitly determine them, if used. See 12.1, 12.4 and 12.8. -end note]

About implicitly declared functions, §12.8 / 8-11:

If the class definition does not explicitly declare the copy constructor and there is no movement declared by the constructor, the copy constructor is implicitly declared as default (8.4).

An implicitly declared copy constructor for class X will be of the form X::X(const X&) if

  • each direct or virtual base class B from X has a copy constructor, the first parameter of which is of type const B& or const volatile B& , and
  • for all non-static X data elements that have the class type M (or their array), each such type of class has a copy constructor, the first parameter of which is of type const M& or const volatile M& .

Otherwise, the constructor with the implicit declaration will have the form X::X(X&) .

If the class definition does not explicitly declare the move constructor, it will be declared as implicit as default, if and only if

  • X does not have a user-declared copy constructor and
  • the move constructor will not be implicitly defined as remote.

[Note. If the move constructor is implicitly declared or explicitly specified, expressions that otherwise call the move constructor may instead call the copy constructor. -end note]

The implicitly declared move constructor for class X will be X::X(X&&) .

About default implicitly deleted functions, §12.8 / 12:

The implicitly declared copy / move constructor is an inline public member of its class. By default, the copy / move constructor for class X is defined as remote (8.4.3) if X has:

  • a variant member with a nontrivial corresponding constructor, and X is the union class,
  • a non-static data member of class M (or its array) that cannot be copied / moved because the overload resolution (13.3) applicable to the corresponding constructor of Ms leads to ambiguity or a function that is removed or inaccessible from the default constructor, or
  • a direct or virtual base class B that cannot be copied / transferred because the overload resolution (13.3) applicable to the corresponding constructor of Bs results in an ambiguity or function that is removed or inaccessible from the default constructor, or
  • for a move constructor, a non-static data element, or a direct or virtual base class with a type that does not have a move constructor and cannot be trivially copied.

§12.8 / 13-18 defines how functions should work when they are implicitly generated.

§12.8 / 19, then does the same as in §12.8 / 8, except for copy and assignment operations. They are similar enough not to quote here.

To get a better picture, you will want to read the entire sections, but this is a general idea. I am glad that we get the semantics of implicit movement.


* But, like the default copy functions, they may not always have the correct behavior! The Big Three should become the Big Five. (For example, the Big Three is implemented whenever we need to copy something deeply. We also need to make sure that we make a “deep move” where the original data is reset / reset. This is not done implicitly.)

+3
source

Source: https://habr.com/ru/post/1316225/


All Articles