std::atomic是否保证int、float等类型初始化为0/0.0?
Does std::atomic guarantee initialization initialization of types like int, float etc to 0/0.0?
std::atomic<basic_type>
是否保证 basic_type
的值在创建为 class 成员时为 0 / 0.0(以适用者为准)而未明确初始化为:
- int / uint / short / ushort / 等...
- 和;浮动/双
?
示例:
class Foo
{
public:
std::atomic<int> bar;
};
int main()
{
Foo foo;
return foo.bar; //foo.bar guaranteed to be 0?
}
来自 std::atomic default constructor 的 cppreference 文档:
The default constructor is trivial: no initialization takes place
other than zero initialization of static and thread-local objects.
std::atomic_init may be used to complete initialization.
因此,在您的情况下,您将获得与简单声明相同的保证 int bar;
std::atomic<basic_type>
是否保证 basic_type
的值在创建为 class 成员时为 0 / 0.0(以适用者为准)而未明确初始化为:
- int / uint / short / ushort / 等...
- 和;浮动/双
?
示例:
class Foo
{
public:
std::atomic<int> bar;
};
int main()
{
Foo foo;
return foo.bar; //foo.bar guaranteed to be 0?
}
来自 std::atomic default constructor 的 cppreference 文档:
The default constructor is trivial: no initialization takes place other than zero initialization of static and thread-local objects. std::atomic_init may be used to complete initialization.
因此,在您的情况下,您将获得与简单声明相同的保证 int bar;