There's no "always better" answer, it's a matter of style.
When initializing an object from a simple expression of the object's type (such as createA()
) I often use copy-init, probably just due to familiarity with the =
form of assignment. Otherwise, when the initializer is a different type or there are multiple initializers for the object (e.g. multiple constructor arguments, or aggregate init) I prefer to use C++11 list-initialization (a.k.a uniform initialization syntax), which can be used in more places, e.g. to initialize aggregates as well as classes with user-defined constructors, and cannot be parsed as a function declaration:
A a1{ createA() };
The above form of list-init uses direct-init, whereas this form uses copy-init:
A a2 = { createA() };
When using list-init I prefer the direct-init form without the redundant =
sign.
There are a few cases where list-init isn't possible, e.g. when a type has an initializer-list constructor (i.e. one taking a parameter that is a specialization of std::initializer_list
) and you want to call a different constructor, but the initializer-list constructor would get chosen, e.g. std::vector<int> v{ 5u, 0 };
will not create a vector of five elements with value zero, but a vector with two elements with values five and zero