4

Consider this code:

struct S
{
    int x;
    double y = 1.1;
};

int main()
{
    S s = {0};
}

According to the C++14 standard, ยง 8.5.1/7

If there are fewer initializer-clauses in the list than there are members in the aggregate, then each member not explicitly initialized shall be initialized from its brace-or-equal-initializer or, if there is no brace-or-equal- initializer, from an empty initializer list (8.5.4).

the code should be perfectly valid.

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++?

vsoftco
  • 55,410
  • 12
  • 139
  • 252
  • 2
    gcc 5.0 [works](http://melpon.org/wandbox/permlink/u87I6PIXszy1sw1K) ... likely a bug that is now fixed โ€“ Shafik Yaghmour Jan 23 '15 at 16:00
  • 3
    `S` is not an aggregate until C++14 (in C++11, aggregates cannot have NSDMIs). If you look at the [GCC C++14 feature table](https://gcc.gnu.org/projects/cxx1y.html) you'll see that this feature isn't implemented until GCC 5. โ€“ T.C. Jan 23 '15 at 16:05
  • @T.C. thanks for the link, should have checked before... โ€“ vsoftco Jan 23 '15 at 16:06

1 Answers1

6

You are correct, this is valid C++14; however, in C++11 a class with in class member initializers was not an aggregate and so this is not valid in C++11.

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

G++ now supports C++14 aggregates with non-static data member initializers.

struct A { int i, j = i; };
A a = { 42 }; // a.j is also 42
Community
  • 1
  • 1
Shafik Yaghmour
  • 154,301
  • 39
  • 440
  • 740