gpt4 book ai didi

c++ - is_trivially_copyable 和 is_trivially_copy_constructible 有什么区别?

转载 作者:IT老高 更新时间:2023-10-28 21:36:34 32 4
gpt4 key购买 nike

这些何时会给出不同的答案,这种差异何时有用(如果有的话)?

最佳答案

前者测试trivially copyable属性,简而言之,这意味着该类型是memcpy-安全的。

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 ]

后者测试普通复制构造函数的存在,顺便说一句,这是普通可复制属性的要求。它基本上意味着该类型的复制构造函数执行按位复制。

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.

为这些特征提供不同结果的类型很容易制造:

struct foo {
foo(foo const&) = default; // this is a trivial copy constructor
~foo(); // this is a non-trivial destructor
};

关于c++ - is_trivially_copyable 和 is_trivially_copy_constructible 有什么区别?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16542291/

32 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com