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?
But apparently std::string is not trivially destructible.
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.
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).
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With