问题
i wrote this program in VC++ 2010:
class class1
{
public:
class1 (initializer_list<int> a){};
int foo;
float Bar;
};
void main()
{
class1 c = {2,3};
getchar();
}
but i get this errors when i compile project:
Error 1 error C2552: 'c' : non-aggregates cannot be initialized with initializer list c:\users\pswin\documents\visual studio 2010\projects\test_c++0x\test_c++0x\main.cpp 27
and
2 IntelliSense: initialization with '{...}' is not allowed for object of type "class1" c:\users\pswin\documents\visual studio 2010\projects\test_c++0x\test_c++0x\main.cpp 27
what is the problem?
回答1:
It shouldn't be supported at all:
[...] the C++0x Core Language feature of initializer lists and the associated Standard Library changes weren't implemented in VC10.
The error message refers to the pre-C++0x feature of aggregate initialization, which allows the initialization of certain user-defined types by using curly braces:
struct pair { int first; char second; };
pair p = { 0, 'c' };
Aggregates are defined in §8.5.1:
An aggregate is an array or a class (clause 9) with no user-declared constructors (12.1), no private or protected non-static data members (clause 11), no base classes (clause 10), and no virtual functions (10.3).
When an aggregate is initialized the initializer can contain an initializer-clause consisting of a brace- enclosed, comma-separated list of initializer-clauses for the members of the aggregate, written in increasing subscript or member order. If the aggregate contains subaggregates, this rule applies recursively to the members of the subaggregate.
来源:https://stackoverflow.com/questions/2792028/initializer-list-not-working-in-vc10