PROBLEM:
You cannot use copy-initialization, because std::atomic_bool is not copy-constructible:
std::atomic_bool World::mStopEvent = false; // ERROR!
In fact, the above is equivalent to:
std::atomic_bool World::mStopEvent = std::atomic_bool(false); // ERROR!
However, you can use direct-initialization:
std::atomic_bool World::mStopEvent(false);
At your wish, you may choose to use braces instead of parentheses:
std::atomic_bool World::mStopEvent{false};
BUG:
While copy-initialization is illegal no matter what compiler you choose, it seems that the implementation of the Standard Library shipped with VC11 has a bug that won't let you perform direct-initialization either.
So how I am supposed to initialize such a variable?
WORKAROUND:
As a possible workaround, you can provide a pair of static getter/setter wrappers that - respectively - set and return the value of the atomic boolean flag, but not before making sure it has been initialized at least once and not more than once to the desired initial value in a thread-safe manner (you can consider this some kind of lazy initialization):
#include <atomic> #include <mutex> struct World { static bool is_stop_event_set() { std::call_once(mStopEventInitFlag, [] () { mStopEvent = false; }); return mStopEvent; } static void set_stop_event(bool value) { std::call_once(mStopEventInitFlag, [value] () { mStopEvent = value; }); mStopEvent = value; } static std::atomic_bool mStopEvent; static std::once_flag mStopEventInitFlag; }; std::atomic_bool World::mStopEvent; std::once_flag World::mStopEventInitFlag;
Now instead of accessing mStopEvent directly, its value shall be read through the is_stop_event_set() function:
#include <iostream> int main() { std::cout << World::is_stop_event_set(); // Will return false }