c++initializationcopy-initialization

Difference copy-initialization and direct-initialization for primitive (scalar) types


as far as I understand in C++ is an initialization in the form

T x = a;

called copy-initialization and an initialization in the form

T x(a);

or

T x{a};

called direct-initialization.

(T...Type, x...variable name, a...expression)

For class types I think the difference is clear (calling copy constructor in case of copy-initialization).

But what if primitive (scalar) types like int are used? Because an int type has no (copy-)constructor which constructor should be called in case of

int x = 5; // copy-initialization

So is there a difference?

int x = 5; // copy-initialization of variable x
int x = {5}; // copy-initialization of variable x
int x(5); // direct-initialization of variable x
int x{5}; // direct-initialization of variable x

What happens here exactly? Or is there no difference if primitive/scalar types are involved and all is syntactic sugar. Similar questions doesnt explain that exactly for me.


Solution

  • There is no difference for primitive scalars like this; the memory location or register (depending on usage) is going to be initialized the same way.