std::is_trivially_copyable

定义于头文件 <type_traits>
template< class T >
struct is_trivially_copyable;
(C++11 起)

T可平凡复制 (TriviallyCopyable) 类型,则提供等于 true 的成员常量 value 。对于任何其他类型, valuefalse

仅有的可平凡复制类型是标量类型、可平凡复制类及这些类型/类的数组(可以为 const 限定,但不可为 volatile 限定)。

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

目录

模板参数

T - 要检查的类型

帮助变量模板

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

继承自 std::integral_constant

成员常量

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

成员函数

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

成员类型

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

注意

可平凡复制的对象是仅有的能以 std::memcpy 安全复制或以 std::ofstream::write()/std::ifstream::read() 序列化自/到二进制文件的对象。通常,可平凡复制的对象是任何底层字节能复制到 char 或 unsigned char 数组,并复制到同类型的新对象,而结果对象与原对象拥有原值的类型。

示例

#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

参阅

(C++11)
检查类型是否平凡
(类模板)

版本历史

  • (当前 | 先前 2017年7月7日 (五) 21:12Fruderica讨论 | 贡献. . (1,983字节) (-1). . (撤销)
  • 当前 | 先前 2017年7月7日 (五) 21:12Fruderica讨论 | 贡献 . . (1,984字节) (0). . (撤销)
  • 当前 | 先前 2017年6月15日 (四) 16:43Fruderica讨论 | 贡献. . (1,984字节) (-182). . (撤销)
  • 当前 | 先前 2014年10月26日 (日) 18:15P12bot讨论 | 贡献 . . (2,166字节) (0). . (Fix some translations) (撤销)
  • 当前 | 先前 2013年7月2日 (二) 11:43P12bot讨论 | 贡献 . . (2,166字节) (-43). . (Use {{lc}}. Update links. Various fixes.) (撤销)
  • 当前 | 先前 2012年11月2日 (五) 15:54P12bot讨论 | 贡献 . . (2,209字节) (+313). . (r2.7.3) (机器人添加:de, en, es, fr, it, ja, pt, ru) (撤销)
  • 当前 | 先前 2012年10月27日 (六) 11:49P12讨论 | 贡献 . . (1,896字节) (0). . (1个修订: Translate from the English version) (撤销)
  • 当前 | 先前 2012年10月26日 (五) 06:00TranslationBot讨论 | 贡献. . (1,896字节) (-14). . (Translated from the English version using Google Translate) (撤销)
  • 当前 | 先前 2012年10月26日 (五) 03:34P12讨论 | 贡献 . . (1,910字节) (0). . (1个修订: Translate from the English version) (撤销)
  • 当前 | 先前) 2012年10月25日 (四) 12:00TranslationBot讨论 | 贡献. . (1,910字节) (+1,910). . (Translated from the English version using Google Translate)