Default Instance Constructor for Class with No Items and Binding Syntax

Consider the following class definition:

class A { };

      

That is, it A

is a class with no data members (although it also has no function members).

The following code works as expected because the compiler generates both a default constructor and a default copy constructor for the class A

:

A foo;
A bar(foo); // calls A default copy constructor

      

However, if you use the binding syntax instead of parentheses. The code doesn't compile:

A foo;
A bar{foo}; // ERROR

      

In GCC 4.9.3 I am getting the error:

too many initializers for 'A'

By doing any of the following, the last piece of code works:

  • Adding a data member to the class definition A

    .
  • Explicitly defining the copy constructor in the class definition A

    (even when used = default

    works). Of course, the default constructor must also be defined after the above code is done, because it is no longer generated by the compiler.

Any ideas why this is happening?

+3


source to share


2 answers


As Zereges suggests in his comment, the issue why the code won't compile is a bug in GCC.



This bug has been fixed in GCC since 6.1 and the code compiles as expected.

0


source


This example can be compiled using gcc 5.1, but only when using C ++ 14, which can be enabled with the -std = C ++ 14 flag.

Looking at http://en.cppreference.com/w/cpp/language/aggregate_initialization you can see in examples:



S s1 = { 1, { 2, 3, {4, 5, 6} } };
S s2 = { 1, 2, 3, 4, 5, 6}; // same, but with brace elision
S s3{1, {2, 3, {4, 5, 6} } }; // same, using direct-list-initialization syntax
S s4{1, 2, 3, 4, 5, 6}; // error in C++11: brace-elision only allowed with equals sign
                        // okay in C++14

      

So it looks like a C ++ 14 feature.

+1


source







All Articles