Before the shouts for duplicate begin: I am aware that the following question (and some others) are quite related to this one:
Is there a difference in C++ between copy initialization and direct initialization?
The answers in this question perfectly explains scenarios where copy initialization is not possible and explains the difference of the two and all that stuff. However, my question is more subtle:
Consider the code:
A createA(){...}
A a1 = createA();
A a2(createA());
Assume that A can be implicitly copy constructed and all that stuff, so both initializations of a1
and a2
are fine. There are no side effects in the copy constructor for A
, so both initializations are also semantically equivalent. createA()
returns directly the type of the variable, not something else that has to be cast first. I think this case is quite common.
So, in this case, where both alternatives are equally applicable and semantically equivalent, which one should I use? Is there a recommendation in the spec or a consensus/best practice in the community or is it just up to me and my coding style which one to use? Has C++11 introduced any difference in comparison to older standards?