std :: atomic的默认值是多少? [英] What's the default value for a std::atomic?
问题描述
我发现在实践中,对于各种C ++ 11/C ++ 14编译器,std::atomic
具有未定义的初始值,就像它是原始"类型一样.也就是说,我们希望表达式
I find that in practice, with a variety of C++11/C++14 compilers, a std::atomic
has an undefined initial value just as it would if it were a "raw" type. That is, we expect that for the expression
int a;
a
可以具有任何值.事实证明,对于表达式
a
may have any value. It also turns out to be true that for the expression
std::atomic< int > b;
b
也可以具有任何值.换句话说,
b
may also have any value. To say it another way,
std::atomic< int > b; // b is undefined
不等同于
std::atomic< int > b{ 0 }; // b == 0
或
std::atomic< int > b{}; // b == 0
因为在后两种情况下b
被初始化为已知值.
because in the latter two cases b
is initialized to a known value.
我的问题很简单:这种行为在C ++ 11或C ++ 14规范中记录在哪里?
My question is simple: where in the C++11 or C++14 spec is this behavior documented?
推荐答案
[atomics.types.generic]/5谈到积分专业:
[atomics.types.generic]/5 says this about integral specializations:
原子积分专业化和专业化原子应具有标准布局.它们每个都有一个琐碎的默认构造函数和一个琐碎的析构函数.他们每个人都应该支持汇总 初始化语法.
The atomic integral specializations and the specialization atomic shall have standard layout. They shall each have a trivial default constructor and a trivial destructor. They shall each support aggregate initialization syntax.
此外,同一部分开头的主要模板提要通常将默认构造函数指定为:
Moreover, the primary template synopsis at the beginning of the same section normatively specifies the default constructor as:
atomic() noexcept = default;
效果在[atomic.types.operations]/4中定义为:
The effects are defined in [atomic.types.operations]/4 as:
效果:使原子对象处于未初始化状态.
Effects: leaves the atomic object in an uninitialized state.
这篇关于std :: atomic的默认值是多少?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!