Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What are the trivial operations in std::is_trivially_copy_constructible in C++

Here's an excerpt from the documentation of std::is_copy_constructible (1) and std::is_trivially_copy_constructible (2) on cppreference.com:

1) Checks whether a type is CopyConstructible, i.e. has an accessible explicit or implicit copy constructor. If the requirement is met, a member constant value equal true is provided, otherwise value is false.

2) Same as (1), but the copy constructor expression does not call any operation that is not trivial.

So what is considered a trivial operation here?

like image 614
vitaut Avatar asked Mar 04 '13 23:03

vitaut


People also ask

Is std:: string trivial?

But apparently std::string is not trivially destructible.

What is a trivial type C++?

Trivial typesWhen a class or struct in C++ has compiler-provided or explicitly defaulted special member functions, then it is a trivial type. It occupies a contiguous memory area. It can have members with different access specifiers. In C++, the compiler is free to choose how to order members in this situation.

What is copy constructible?

A copy constructible type is a type that can be constructed from a value or reference of the same type. This includes scalar types and copy constructible classes. A copy constructible class is a class that has a copy constructor (either its implicit constructor or a custom defined one).

Is trivially copy constructible?

A trivially copy constructible type is a type which can be trivially constructed from a value or reference of the same type. This includes scalar types, trivially copy constructible classes and arrays of such types.


1 Answers

As I put it once before:

So, what are all those trivial and non-trivial things?

A copy/move constructor for class X is trivial if it is not user-provided and if

— class X has no virtual functions (10.3) and no virtual base classes (10.1), and

— the constructor selected to copy/move each direct base class subobject is trivial, and

— for each non-static data member of X that is of class type (or array thereof), the constructor selected to copy/move that member is trivial;

otherwise the copy/move constructor is non-trivial.

Basically this means that a copy or move constructor is trivial if it is not user-provided, the class has nothing virtual in it, and this property holds recursively for all the members of the class and for the base class.

like image 183
2 revs Avatar answered Oct 14 '22 21:10

2 revs