Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is struct A { int a[2] = {1, 2}; }; legal as per C++11?

The following code is accepted by clang 3.4

struct A
{
    int a[2] = {1, 2};
};

However, vc++ 2014 CTP rejects it with an error message:

error C2536: 'A::A::a': cannot specify explicit initializer for arrays

Which compiler is correct?

like image 652
xmllmx Avatar asked Feb 02 '26 05:02

xmllmx


1 Answers

Yes, It is legal but VC++ does not support this feature. It is the uniform initialization of an in-class member initialization.

Of course the = sign is optional and your code is the same as the following code:

struct A
{
    int a[2] {1, 2};
};
like image 84
Sadeq Avatar answered Feb 04 '26 18:02

Sadeq



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!