Consider the following class definition:
class A { };
That is, A
is a class without data members (even though it doesn't have function members either).
The following code work as expected because the compiler generates both the default constructor and the default copy constructor for the class A
:
A foo;
A bar(foo); // calls A's default copy constructor
However, if the brace syntax is used instead of the parenthesis. The code doesn't compile:
A foo;
A bar{foo}; // ERROR
On GCC 4.9.3 I get the error:
too many initializers for 'A'
By doing any of the following points, the last code snippet does work:
A
class definition.A
class definition (not even by using = default
works). Of course, the default constructor has also to be defined after doing so for the code above to work, because it is not generated by the compiler any more.Any ideas why is this happening?
As Zereges suggests in his comment, the issue why the code is not compiling is a bug in GCC.
This bug is fixed in GCC as of version 6.1 and the code compiles as expected.