Why does initializing std::atomic_flag with false work on GCC and Clang but not Visual Studio?

23 hours ago 2
ARTICLE AD BOX

std::atomic_flag is a strange thing mostly copied from C. It cannot be initialised in any way except for a special macro ATOMIC_FLAG_INIT, which initialises the object to false state.

ATOMIC_FLAG_INIT is implementation specific, and gcc defines it as {0} while MSVC defines it as {}. That means gcc decided to add a constructor taking int (or something convertible to int) and using bool works as well. MSVC didn't add such a constructor, so you cannot use bool to initialise it. See preprocessed output from both compilers on Godbolt.

In C++20 the default constructor finally does the sensible thing and initialises the object to false state instead of leaving it unspecified. Before C++20, the only portable way is to use ATOMIC_FLAG_INIT.

Read Entire Article