What is difference between initializing x1
and x2
?
struct X {
int i;
};
void func(){
X x1 = {2};
X x2 {2};
cout << x1.i << ", " << x2.i << endl;
}
What is difference between initializing x1
and x2
?
struct X {
int i;
};
void func(){
X x1 = {2};
X x2 {2};
cout << x1.i << ", " << x2.i << endl;
}
X x1 = {2};
Is copy-list-initialization.
X x2 {2};
Is direct-initialization.
Both syntaxes perform aggregate initialization, because X
is an aggregate.
What is difference
Braces can not be elided with the direct-initialization form (until C++14).
In general, copy initialization only considers non-explicit constructors and conversion functions and direct initialization considers also explicit ones. However, this does not apply to aggregate initialization.
with and without = operator
The equals (=) character here isn't actually an operator. It is part of the syntax of copy initialization.