Search code examples
c++11initializationstdatomic

Does std::atomic guarantee initialization initialization of types like int, float etc to 0/0.0?


Does std::atomic<basic_type> guarantee the value of basic_type to be 0 / 0.0 (whichever is applicable) when created as a class member without being explicitely initialized for:

  • int / uint / short / ushort / etc...
  • and; float / double

?

Example:

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

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

Solution

  • from cppreference documentation of std::atomic default constructor:

    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.

    Hence in your case you will have the same guarantees as if you had declared simply int bar;