且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

为什么std :: atomic的默认构造函数不默认初始化底层存储值?

更新时间:2023-11-10 23:03:40

",此行为的主要原因是与C兼容. atomic_int i; 不执行初始化.为了与C兼容,等效的C ++也必须不执行任何初始化.而且由于C ++中的 atomic_int 应该是 std :: atomic< int> 的别名,因此,为了实现完全的C/C ++兼容性,该类型也必须不执行任何初始化.

As mentioned in P0883, the main reason for this behavior is compatibility with C. Obviously C has no notion of value initialization; atomic_int i; performs no initialization. To be compatible with C, the C++ equivalent must also perform no initialization. And since atomic_int in C++ is supposed to be an alias for std::atomic<int>, then for full C/C++ compatibility, that type too must perform no initialization.

幸运的是, C ++ 20似乎正在消除这种行为.