G ++ 4.9 rejects actual aggregate initialization in C ++ 14

Consider this code:

struct S { int x; double y = 1.1; }; int main() { S s = {0}; } 

In accordance with C ++ 14, ยง 8.5.1 / 7

If there are fewer initializer sentences in the list than in the aggregate, then each member that is not explicitly initialized should be initialized from its element with an equal-equal-initializer or, if there are no brackets or equal, an initializer from an empty initializer list (8.5.4) .

the code should be completely correct.

However, g ++ 4.9.2 rejects the code (compiled with -std=c++14 )

 so.cpp:9:13: error: could not convert '{0}' from '<brace-enclosed initializer list>' to 'S' S s = {0}; 

clang ++ on the other hand compiles it.

Is this a known issue for g ++?

+3
c ++ g ++ c ++ 14
Jan 23 '15 at 15:57
source share
1 answer

You are right, this is really C ++ 14; however, in C ++ 11, class c in class initializers was not an aggregate , and therefore this is not valid in C ++ 11.

The problem, as I noted in my answer to the above question, and I realized that after I made my initial comment, gcc did not support this change until 5.0 ( watch it live ):

g ++ now supports C ++ 14 aggregates with a non-static data member Initializers.

 struct A { int i, j = i; }; A a = { 42 }; // aj is also 42 
+5
Jan 30 '15 at 16:08
source share



All Articles