A few days ago I've written something like the following:
struct A {
std::atomic_bool b = false;
};
Compiled in Visual Studio 2015 Update 3 with its VC++2015 compiler, nothing wrong popped up.
Now I've recompiled the same thing with GCC (5.4.0) on Ubuntu and got the error:
use of deleted function 'std::atomic::atomic(const std::atomic&)
I got the same error on ideone, set to C++14 (not sure what compiler version it is using).
Of course changing the code to the following fixed the problem with gcc:
struct A {
std::atomic_bool b { false };
};
My questions are:
1. who is is right (C++11 compliant) here, VC++ or GCC? It seems that VC++ calls the constructor from bool, while GCC calls copy constructor (deleted).
2. For the purpose of default value initializing an atomic in a class declaration, is uniform initialization (above) the correct/preferred way? Or should I use ATOMIC_VAR_INIT macro (ugh!) instead?
struct A {
std::atomic_bool b = ATOMIC_VAR_INIT(false);
};
ATOMIC_VAR_INIT
, that's mostly for C11 compatibility. – Satori