Namespaces
Variants

std:: is_trivially_copyable

From cppreference.net
Metaprogramming library
Type traits
Type categories
(C++11)
(C++11) ( DR* )
Type properties
(C++11)
(C++11)
(C++14)
(C++11) (deprecated in C++26)
is_trivially_copyable
(C++11)
(C++11) ( until C++20* )
(C++11) (deprecated in C++20)
(C++11)
Type trait constants
Metafunctions
(C++17)
Supported operations
Relationships and property queries
Type modifications
Type transformations
(C++11) (deprecated in C++23)
(C++11) (deprecated in C++23)
(C++11)
(C++11) ( until C++20* ) (C++17)

Compile-time rational arithmetic
Compile-time integer sequences
定义于头文件 <type_traits>
template < class T >
struct is_trivially_copyable ;
(C++11 起)

std::is_trivially_copyable 是一个 UnaryTypeTrait

如果 T 可平凡复制 类型,则提供等于 true 的成员常量 value 。对于其他任何类型, value false

如果 std:: remove_all_extents_t < T > 是不完整类型且不是(可能带有 cv 限定符的) void ,则行为未定义。

如果程序为 std::is_trivially_copyable std::is_trivially_copyable_v 添加特化,则行为是未定义的。

目录

模板参数

T - 待检查的类型

辅助变量模板

template < class T >
constexpr bool is_trivially_copyable_v = is_trivially_copyable < T > :: value ;
(自 C++17 起)

继承自 std:: integral_constant

成员常量

value
[static]
T 为可平凡复制类型则为 true ,否则为 false
(公开静态成员常量)

成员函数

operator bool
转换对象为 bool 类型,返回 value
(公开成员函数)
operator()
(C++14)
返回 value
(公开成员函数)

成员类型

类型 定义
value_type bool
type std:: integral_constant < bool , value >

注释

非潜在重叠子对象的平凡可复制类型对象是唯一可以通过 std::memcpy 安全复制的 C++ 对象,或是可通过 std::ofstream::write() / std::ifstream::read() 序列化到二进制文件/从二进制文件反序列化的 C++ 对象。

示例

#include <type_traits>
struct A { int m; };
static_assert(std::is_trivially_copyable_v<A> == true);
struct B { B(B const&) {} };
static_assert(std::is_trivially_copyable_v<B> == false);
struct C { virtual void foo(); };
static_assert(std::is_trivially_copyable_v<C> == false);
struct D
{
    int m;
    D(D const&) = default; // -> 可平凡复制
    D(int x) : m(x + 1) {}
};
static_assert(std::is_trivially_copyable_v<D> == true);
int main() {}

缺陷报告

以下行为变更缺陷报告被追溯应用于先前发布的C++标准。

缺陷报告 适用范围 发布时行为 正确行为
LWG 2015 C++11 T 可能为未知边界的
不完整类类型的数组
此情况下行为
未定义

参见

(C++11) (deprecated in C++26)
检查类型是否为平凡类型
(类模板)