nikitablack
nikitablack

Reputation: 4663

In place member initialization and aggregate initialization

I have this simple struct and a function taking it:

struct S
{
    int a;
};

void foo(S){}

foo({5});

This works fine.

But if I change int a; to int a{0}; VisualStudio (2013 and 2015) complains:

error C2664: 'void foo(S)': cannot convert argument 1 from 'initializer list' to 'S'

I can't find corresponding rule for this in the documentation. But both gcc and clang accept this without problem.

Upvotes: 2

Views: 362

Answers (1)

Jarod42
Jarod42

Reputation: 218343

struct S
{
    int a;
};

is an aggregate whereas

struct S
{
    int a {0}; // or int a = 0;
};

is not an aggregate in c++11, but is in c++14.

VisualStudio (2013 and 2015) still uses the c++11 rules in this regard.

foo({5}); is valid for aggregate. For non aggregate, it will (try to) call appropriate constructor, but S doesn't have one valid for this argument.

Upvotes: 2

Related Questions