std::is_trivially_copyable

来自cppreference.com
< cpp‎ | types

 
 
 
类型的支持
基本类型
基础类型
定宽整数类型 (C++11)
数值极限
C 数值极限接口
运行时类型信息
类型特性
类型类别
(C++11)
(C++11)
(C++11)
(C++11)
(C++11)
(C++11)
(C++11)
(C++11)
类型属性
(C++11)
(C++11)
(C++11)
(C++14)
(C++11)
is_trivially_copyable
(C++11)
(C++11)(C++17中过时)
(C++11)
类型特性常量
元函数
(C++17)
受支持操作
关系与属性查询
(C++11)
(C++11)
(C++11)
(C++11)
类型修改
(C++11)
(C++11)
(C++11)
类型变换
(C++11)
(C++11)
(C++17)
(C++11)(C++17中过时)
(C++17)
 
定义于头文件 <type_traits>
template< class T >
struct is_trivially_copyable;
(C++11 起)

If T is a trivially copyable type, provides the member constant value equal true. For any other type, value is false.

The only trivially copyable types are scalar types, trivially copyable classes, and arrays of such types/classes (possibly cv-qualified).

A trivially copyable class is a class that

1. Has no non-trivial copy constructors (this also requires no virtual functions or virtual bases)

2. Has no non-trivial move constructors

3. Has no non-trivial copy assignment operators

4. Has no non-trivial move assignment operators

5. Has a trivial destructor

目录

继承自 std::integral_constant

成员常量

value
[静态]
T is a trivially copyable type 则为true,否则为false
(公开静态成员常量)

成员函数

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

成员类型

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

[编辑] 注释

Objects of trivially-copyable types are the only C++ objects that may be safely copied with std::memcpy or serialized to/from binary files with std::ofstream::write()/std::ifstream::read(). In general, a trivially copyable type is any type for which the underlying bytes can be copied to an array of char or unsigned char and into a new object of the same type, and the resulting object would have the same value as the original.

[编辑] 示例

#include <iostream>
#include <type_traits>
 
struct A {
    int m;
};
 
struct B {
    B(const B&) {}
};
 
struct C {
    virtual void foo();
};
 
int main()
{
    std::cout << std::boolalpha;
    std::cout << std::is_trivially_copyable<A>::value << '\n';
    std::cout << std::is_trivially_copyable<B>::value << '\n';
    std::cout << std::is_trivially_copyable<C>::value << '\n';
}

输出:

true
false
false