问题
struct X
{
int a;
int b;
};
int f(X x)
{
return x.a + x.b;
}
int main()
{
int n = f({1, 2});
}
Visual Studio 2012 (Nov CTP) reports:
error C2664: 'int f(const X &)' : cannot convert parameter 1 from
'initializer-list' to 'X'
Reason: cannot convert from 'initializer-list' to 'X'
Only an initializer-list with zero or one elements can be converted to this type
Build FAILED.
回答1:
Visual Studio 2012 (Nov CTP) reports:
It's not even a beta compiler. It's supposed to work. I'd link to your code working on ideaone, but this website won't let be do that.
File a bug report on it.
回答2:
If you want to pass intializer list style syntax to your constructor then you have to pass a std::initializer_list type to your constructor. This type will then construct an array of your types when passed a { }
syntax style construct.
来源:https://stackoverflow.com/questions/14013135/why-can-an-initializer-list-not-be-used-as-an-argument