[英]Why is std::atomic_ref not implemented in terms of std::atomic
The referenced implementation of std::atomic_ref from the paper P0019r8 roughly keeps the template type as a member variable ( https://github.com/ORNL/cpp-proposals-pub/blob/master/P0019/atomic_ref.hpp ) and uses the GNU built-ins to implement atomic operations. 从论文P0019r8引用的std :: atomic_ref的实现大致将模板类型保留为成员变量( https://github.com/ORNL/cpp-proposals-pub/blob/master/P0019/atomic_ref.hpp )并使用GNU内置程序可实现原子操作。
The question I have here is - why not reinterpret_cast
to std::atomic
and use the atomic operations instead? 我在这里的问题是-为什么不
reinterpret_cast
为std::atomic
并使用原子操作呢? Is there a portability concern or detail I am missing? 我是否缺少便携性或细节?
重新解释某种东西而不使用它是不确定的行为。
There's no guarantee whatsoever that a std::atomic<T>
contains nothing but a T
and has the same size and alignment requirements as a T
. 有没有保证,一个
std::atomic<T>
包含只是一个T
,并有大小和对齐要求为同一T
。 For example, if sizeof(T) == 3
, an implementation of std::atomic<T>
may pad it to 4 bytes to enable the use of intrinsics. 例如,如果
sizeof(T) == 3
,则std::atomic<T>
可以将其填充到4个字节以启用内部函数。 For another example, if sizeof(T)
is too big for an intrinsic, std::atomic<T>
might store a synchronization primitive of some sort to serialize the operation. 再举一个例子,如果
sizeof(T)
对于内在函数而言太大,则std::atomic<T>
可能会存储某种同步原语以序列化该操作。
It follows that reinterpret_cast
to std::atomic
is not a viable implementation in the general case even if you ignore the general undefined behavior from the object model violations. 因此,即使您从对象模型违规中忽略了一般未定义的行为,通常情况下
reinterpret_cast
到std::atomic
也不可行。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.