MSVC 2013 complains about the following code, while it works as expected in g++. Does this look like a bug in MSVC?
#include <iostream>
using namespace std;
struct A
{
double x = 0.0, y = 0.0;
};
int main()
{
A a{ 1.0, 2.0 };
return 0;
}
Note that changing the struct
as follows resolves the issue.
struct A
{
double x, y;
};
The error message is:
Error 1 error C2440: 'initializing' : cannot convert from 'initializer-list' to 'A'
Actually, Visual Studio is correct.
Your class is not an aggregate, so aggregate initialisation may not be used on it:
And I shan't quote it all, but
[C++11: 8.5.4/3]
, where list-initialization is defined, is where our story ends. It shows that without an initializer-list constructor, and given that your list has two elements (not one and not zero), your program is ill-formed.GCC actually doesn't accept your program (example thanks to Igor), though clang erroneously does (example, same credit).
Yep, time to stop doing that, if you want your C++11 classes to be aggregates. :)
C++14 actually removed the brace-or-equal-initializers restriction from
8.5.1/1
, so switching to a newer standard will get you where you want to be.