I recently discovered weird clang and gcc behavior. I have a struct ( MyClass ) that uses class initialization for one of its members ( active ):
struct MyClass { int something; bool active = true; };
Now I am trying to copy the initialization of this class.
Using clang, I can decide whether I include active in the initializer list ( MyClass a = { 42, true}; ) or not ( MyClass a = { 42 }; ).
However, using gcc, my code compiles if I do not enable active . Otherwise, I will get the following compiler error:
error: could not convert '{42, true}' from '<brace-enclosed initializer list>' to 'MyClass'
This is mistake? What does the standard say about this? What about VSC ++? Which method would you recommend as a portable solution?
Tested using gcc 4.9 and clang 3.5 on Debian Linux.
source share