What is the difference between direct initialization and uniform initialization in C++?
What is the difference between writing
int a{5}; // Uniform
and
int a(5); // Direct
What is the difference between direct initialization and uniform initialization in C++?
What is the difference between writing
int a{5}; // Uniform
and
int a(5); // Direct
In this particular example there will be no difference due to the type and the value choosen:
int
and5
.In some other cases what initialization means does depend on whether we use
{}
or()
. When we use parenthesis, we're saying that the values we supply are to be used to construct the object, making a computation. When we use curly braces, we're saying that (if possible) we want to list initialize the object; If it is not possible to list initialize the object, the object will be initialized by other means.E.g.
For a built-in type, such as
int
, the{}
will have another function:For more information you may check the cppreference.com - Initialization