le migou
le migou

Reputation: 471

Brace initialization of scalars inside an aggregate

Knowing that this is valid c++11

int i {1};

is this one valid?

int j[] {{1}};

GCC gives an error, clang a warning.

Upvotes: 2

Views: 126

Answers (1)

TemplateRex
TemplateRex

Reputation: 70516

This is not valid because j is an array of int (scalars). You can only use embedded braces for members which are aggregates themselves.

#include <initializer_list>

struct T
{
    int x, y;    
};

int main() 
{
    int j[] {{1}}; // error, int is scalar
    T t[] {{1,2}}; // OK, T is aggregate
}

Upvotes: 2

Related Questions