I defined the following class:
class A {
int a;
public:
A(int _a = 0) :a(_a){}
A(initializer_list<int> il) :a(il.size()){}
friend A operator+(const A& a1, const A& a2);
};
A operator+(const A& a1, const A& a2){ return A(); }
The following customer code
A a;
operator+(a,{3, 4, 5});
may compile but the following
A a;
a + {3, 4, 5};
cannot compile. Error message " error C2059: syntax error : '{'" and " error C2143: syntax error : missing ';' before '{'".
Both two client codes try to perform implicit type conversion from the initialization list {3,4,5}to class A, but the first is executed while the second fragment does not work. I do not understand why.
Can you explain this?
I am using MS Visual Studio 2013 4 update.
[]
, . , :
, -init-list RHS , . , , , arg1 + arg2, operator+ (arg1, arg2), arg1 . , . , , , - , , . , . , , , - ++? , .