2

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.

nikitablack
  • 4,359
  • 2
  • 35
  • 68

1 Answers1

2
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.

Jarod42
  • 203,559
  • 14
  • 181
  • 302