-1

当作为类成员创建时,std::atomic<basic_type>保证值为0 / 0.0(以适用者为准),而无需显式初始化:basic_type

  • int / uint / short / ushort / 等等...
  • 和; 浮动/双

?

例子:

class Foo
{
public:
    std::atomic<int> bar;
};

int main()
{
    Foo foo;
    return foo.bar; //foo.bar guaranteed to be 0?
}
4

1 回答 1

1

来自std::atomic 默认构造函数的 cppreference 文档:

默认构造函数是微不足道的:除了静态和线程本地对象的零初始化之外,不进行任何初始化。std::atomic_init 可用于完成初始化。

因此,在您的情况下,您将获得与您简单声明一样的保证int bar;

于 2016-04-15T09:07:17.360 回答