我可以memcpy()任何类型有一个小的析构函数? [英] Can I memcpy() any type which has a trivial destructor?
问题描述
I do 意识到 is_pod
是类型为 memcpy
-able,但是 has_trivial_destructor
足以达到这个目的吗?如果没有,为什么?
否。要求是该类型为 trivially可复制(§3.9/ 2),其中几个更多的要求,像缺少一个非平凡的复制构造函数(§9/ 6)。
一个简单的可复制类是一个类:
- 没有非平凡的复制构造函数),
- 没有不平凡的移动构造函数(12.8),
副本赋值运算符(13.5.3,12.8),
- 没有非平凡的移动赋值运算符(13.5.3,12.8)和
- 有一个小的析构函数(12.4)。
所以你应该使用 is_trivially_copyable
。
I do realize is_pod
is a sufficient condition for a type to be memcpy
-able, but is has_trivial_destructor
also sufficient for this purpose? If not, why?
No. The requirement is that the type be trivially copyable (§3.9/2) which has a few more requirements, like the lack of a non-trivial copy constructor (§9/6).
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).
So you should use is_trivially_copyable
instead.
这篇关于我可以memcpy()任何类型有一个小的析构函数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!