在什么情况下,我们需要知道一个类是否是微不足道的? [英] In what cases do we need to know if a class is trivial?
问题描述
std :: is_trival< T> :: value
可以确定类T是否不重要。
std::is_trival<T>::value
can determine if class T is trivial. However, I cannot think of a scenario that needs this information.
有没有什么例子?
有些想法:
假设T类是微不足道的,是否可以通过 memcpy
:
Provided that class T is trivial, does it mean T can be safely copied by memcpy
like this:
T t1, t2;
memcpy(&t1, &t2, sizeof(T));
?
推荐答案
如果类型不重要,可以使用 memcpy
复制。这是用作 std :: atomic
模板的参数的用户定义类型的要求。也就是说,对于用户定义的类型 T
,允许原子
(并且对于更大的类型,需要)实现从 T
类型的对象到原子
的对象的赋值 memcpy
。同样,当需要时,用 memcmp
来测试是否相等。
If a type is trivial it can, for example, be copied with memcpy
. That's a requirement for user-defined types that are used as arguments to the std::atomic
template. That is, for a user-defined type T
, atomic<T>
is allowed (and, for larger types, required) to implement assignment from an object of type T
to an object of type atomic<T>
with memcpy
. Similarly, testing for equality, when needed, is done with memcmp
.
使用这些C函数的原因的C ++操作符使得原子操作不会对用户代码进行任何调用。这样做可能导致从无辜的代码的神秘的死锁。
The reason for using these C functions instead of the C++ operators is so that atomic operations do not make any calls into user code. Doing that could lead to mysterious deadlocks from innocent-looking code.
这篇关于在什么情况下,我们需要知道一个类是否是微不足道的?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!