The following two code segments presents the task of initializing variable b as a copy of a. The first code segment initializes the variable using copy initialization (initialization using =). Assume class Apple is simply defined as an empty class: class Apple {};
Apple a;
Apple b = a;
The second code segment initializes the variable using copy initialization as well. Though what is copied in the initialization is the copy of a.
Apple a;
Apple b = Apple(a);
When reading this blindly, it seems as if a copy happens at Apple(a), and another at Apple b = .... In contradiction, overriding the copy constructor of Apple to print something on copy shows that only one copy happens during Apple b = Apple(a).
Are the two statements Apple b = a; and Apple b = Apple(a); identical? Are there instances where they are not identical?
Yes, in concept, for
Apple b = Apple(a);, a temporaryAppleis constructed fromafirstly, thenbis copy-initialized from the temporary. Because of copy elision, as the effectbis initialized fromadirectly.This kind of copy elision is guaranteed since C++17, before C++17 it's an optimization. Compiling with pre-C++17 mode and option prohibiting optimization, you might see the difference between the two cases.
LIVE