Yes, for built in types int x = 1;
and int x(1);
are the same.
When constructing objects of class type then the two different initialization syntaxes are subtly different.
Obj x(y);
This is direct initialization and instructs the compiler to search for an unambiguous constructor that takes y
, or something that y
can be implicitly converted to, and uses this constructor to initialize x.
Obj x = y;
This is copy initialization and instructs the compiler to create a temporary Obj
by converting y
and uses Obj
's copy constructor to initalize x
.
Copy initalization is equivalent to direct initialization when the type of y
is the same as the type of x
.
For copy initalization, because the temporary used is the result of an implicit conversion, constructors marked explicit
are not considered. The copy constructor for the constructed type must be accessible but the copy itself may be eliminated by the compiler as an optmization.