Why does Visual C ++ 2015 allow std :: atomic assign?

A few days ago I wrote something like the following:

struct A { std::atomic_bool b = false; }; 

Compiled in Visual Studio 2015 Update 3 with its VC ++ 2015 compiler, nothing bad happened.
Now I recompiled the same with GCC (5.4.0) on Ubuntu and got an error:

using the remote function 'std :: atomic :: atomic (const std :: atomic &)

I got the same error on ideone , installed on C ++ 14 (not sure which version of the compiler it uses).

Of course, when changing the code to the following, the problem with gcc was fixed:

 struct A { std::atomic_bool b { false }; }; 

My questions:
1. Who is right (compatible with C ++ 11) here, VC ++ or GCC? It seems that VC ++ calls the constructor from bool, and GCC calls the copy constructor (is deleted).
2. For the default initializing atom in the class declaration, uniform initialization (above) of the correct / preferred method? Or should I use the ATOMIC_VAR_INIT (ugh!) Macro instead?

 struct A { std::atomic_bool b = ATOMIC_VAR_INIT(false); }; 
+8
c ++ gcc c ++ 11 stdatomic visual-c ++ - 2015
source share
1 answer

VC is wrong here. Pre-C ++ 17 semantically the code X x = y means a call to X tmp(y) , followed by a call to X(tmp) - i.e. There is a semantically called constructor instance.

While all the compilers I know, if you exclude the intermediate call (the standard allows this), the program is still poorly formed. VC does not seem to provide the correct semantics.

In C ++ 17, this call semantics will change and require only one call to the initialization constructor, so the code will be well-formed.

+3
source share

All Articles