What rules determine if an object is trivially copied

With the introduction of C ++ 11, trivial copyability has become quite relevant. Most noticeable is the use of "std :: atomic". The basics are pretty simple. The foo class can be trivially copied if:

 foo* src = new foo(); foo* dest = malloc(sizeof(foo)); memcpy(dest, src, sizeof(foo)); 

It has the same effect as:

 foo* src = new foo(); foo* dest = new foo(src); 

Thus, an object in which copying memory will have the same effect as the copy constructor. However, of course, this is a catch. Constructors are not only copied there. But also move constructors, move assignment operators. Etc.

std :: is_trivially_copyable can be used to check if an object is trivially copied. Thus, with trial and error, you can make the object trivially copied.

But, of course, a well-defined set of rules will be a little nicer :). So my request.

+5
source share
1 answer

The most clearly defined set of rules will come directly from the standard. Here are the relevant entries from the standard draft N4296:

Trivially copied types are defined in [basic.types] / 9

Cv-unskilled scalar types, trivially-copied class types, arrays of such types, and non-volatile Const-certified versions of these types are collectively called trivially-copied types.

Trivially copied classes are defined in [class] / 6

A trivially copied class is a class that: does not have non-trivial copy constructors, does not have nontrivial move constructors, does not have nontrivial copy assignment operators, does not have nontrivial move assignment operators, and has a trivial destructor.

Copy / move constructors in [class.copy] / 12

The copy / move constructor for class X is trivial if it is not provided to users, its list of parameter parameters is equivalent to the list of parameter type of an implicit declaration, and if class X has no virtual functions and no virtual base classes, and class X has no non-static members data of type volatile-qualty, and the constructor selected for copying / moving each subobject of the direct base class is trivial for every non-static data element X that has the class type (or its array) selected constructor copy / move this element is trivial; otherwise, the copy / move constructor is nontrivial.

Copy / move assignment operators in [class.copy] / 25

The copy / move assignment operator for class X is trivial if it is not provided to users, its list of parameter-parameters is equivalent to the list of parameter-type of implicit declaration, and if class X has no virtual functions and there are no virtual base classes, and class X has no non-static members data of type volatile-qualty, and the assignment operator selected to copy / move each subobject of the direct base class is trivial for every non-static data element X that has the class type a (or array), the assignment operator selected for copy / move this element is trivial; otherwise, the copy / move assignment operator is nontrivial.

Destructors in [class.dtor] / 5

A destructor is trivial if it is not provided to the user and if: the destructor is not virtual, all direct base classes of its class have trivial destructors, for all non-static data members of its class that are of the class type (or its array), each such class has a trivial destructor . Otherwise, the destructor is nontrivial.

Custom Constructors in [dcl.fct.def.default] / 5

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 his first declaration. A user-provided function with explicit default (i.e., obviously defaulted after its first declaration) is defined in the place where it is clearly defaulted; if such a function is implicitly defined as deleted, the program is poorly formed.

The short answer is that a short answer is sometimes more useful than a long answer.

+10
source

All Articles