is_trivially_copyable和is_trivially_copy_constructible有什么区别? [英] What is the difference between is_trivially_copyable and is_trivially_copy_constructible?
问题描述
这些给出不同的答案,何时会有用,如果有的话。
测试 属性。
一个简单的可复制类是一个类:
- 没有非平凡的复制构造函数(12.8) p>
- 没有非平凡的移动构造函数(12.8),
- 没有非平凡的复制赋值运算符13.5.3,12.8),
- 没有非平凡的移动分配运算符(13.5.3,12.8)和
- 有一个简单的析构函数(12.4)。
一个简单的类是一个有一个琐碎的默认构造函数(12.1) / p>
[注意:特别是,一个简单的可复制或简单的类没有虚函数
或虚拟基类。 -end note ]
后者测试是否存在这顺带地是平凡可复制属性的要求。
类X的复制/移动构造函数是微不足道的,如果它是不是用户提供的,如果
- 类X没有虚拟函数(10.3),没有虚拟基类(10.1)和
- 为每个非静态数据成员选择复制/移动每个直接基类子对象的构造函数是微不足道的,
(或其数组),选择复制/移动该成员的构造函数
是微不足道的;
否则复制/移动构造函数是不平凡的。
很容易制作出能为这些特征提供不同结果的类型:
struct foo {
foo(foo const&)= default; //这是一个平凡的复制构造函数
〜foo(); // this is a non-trivial destructor
};
When would these give a different answer, and when would this difference be useful, if at all?
The former tests for the trivially copyable property, which in few words means that the type is memcpy
-safe.
A trivially copyable class is a class that:
— has no non-trivial copy constructors (12.8),
— has no non-trivial move constructors (12.8),
— has no non-trivial copy assignment operators (13.5.3, 12.8),
— has no non-trivial move assignment operators (13.5.3, 12.8), and
— has a trivial destructor (12.4).
A trivial class is a class that has a trivial default constructor (12.1) and is trivially copyable.
[ Note: In particular, a trivially copyable or trivial class does not have virtual functions or virtual base classes.—end note ]
The latter tests for the presence of a trivial copy constructor, which incidentally is a requirement for the trivially copyable property. It basically implies that the copy constructor for the type performs a bitwise copy.
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.
It is easy to fabricate a type that provides different results for these traits:
struct foo {
foo(foo const&) = default; // this is a trivial copy constructor
~foo(); // this is a non-trivial destructor
};
这篇关于is_trivially_copyable和is_trivially_copy_constructible有什么区别?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!