I l@ve RuBoard |
Solution
Direct initialization means the object is initialized using a single (possibly conversion) constructor, and is equivalent to the form "T t(u);":
Copy initialization means the object is initialized using the copy constructor, after first calling a user-defined conversion if necessary, and is equivalent to the form "T t = u;":
Aside: The reason for the "or similar" equivocation is that the copy and conversion constructors could take something slightly different from a plain reference (the reference could be const or volatile, or both), and the user-defined conversion constructor or operator could, in addition, take or return an object rather than a reference. Also, the copy and conversion constructors could have additional default arguments. Note that in the last case ("T t3 = u;") the compiler could call both the user-defined conversion (to create a temporary object) and the T copy constructor (to construct t3 from the temporary), or it could choose to elide the temporary and construct t3 directly from u (which would end up being equivalent to "T t3(u);"). Either way, the unoptimized code must still be legal. In particular, the copy constructor must still be accessible, even if the call to it is optimized away. In the C++ standard, the compiler's latitude to elide temporary objects has been restricted compared with the latitude compilers enjoyed in pre-standard times. Elision is still allowed, however, for this optimization and for the return value optimization. For more details, see Exceptional C++ [Sutter00] Item 42 about the basics, and Exceptional C++ Item 46 covering the change in the final standard. Guideline
In the standard, the thrilling section 8.5 covers most of these. There were also three tricks that don't involve initialization at all. Did you catch them?
Base and member initialization both use direct initialization.
Passing and returning values both use copy initialization.
Trick: No initialization of a new object is involved in these cases. Only references are created.
A static_cast uses direct initialization.
Throwing and catching an exception object both use copy initialization. Note that in this particular code, there are two copies, for a total of three T objects. A copy of the thrown object is made at the throw site. And in this case, a second copy is made because the handler catches the thrown object by value. In general, though, prefer to catch exceptions by reference, not by value, to avoid making extra copies and to eliminate potential object slicing.[1]
This function-style cast uses direct initialization.
Brace-enclosed initializers use copy initialization.
Finally, new expressions use direct initialization. |
I l@ve RuBoard |
No comments:
Post a Comment