temporaries
I am writing in English to save many language switches: The quotations from the C++ standard were taken from its official text (18USD for a PDF file): I searched for the word "temporary". The last version of the draft can be found (free) at Bjarne Stroustrup's page below. About creating temporaries, here is C++ standard 3.10(6): An expression which holds a temporary object resulting from a cast to a nonreference type is an rvalue (this includes the explicit creation of an object using functional notation (5.2.3)). When you hold a value in a register without writing it into the memory, it is _always_ an optimization. The fact is that if you have 128 registers you are able to perform more optimizations than one that has only 8. C++ is a general and portable language (though not in the sense of Java, and Java is not 100% portable), and therefore C++ describes its semantics without referring to any number of registers, or other optimization techniques. Nevertheless, C++ does not ignore opportunities for optimizations. About const reference parameter to function, C++ 5.2.2(5): Where a parameter is of const reference type a temporary object is introduced if needed (7.1.5, 2.13, 2.13.4, 8.3.4, 12.2). The following says that int(c) is the same as (int)c: C++ 5.2.3[explicit type conversion (functional notation)](1): ... If the expression list is a single expression, the type conversion expression is equivalent (in definedness, and if defined in meaning) to the corresponding cast expression (5.4). Static cast is the same (5.2.9) [I save you the quotation]. C++ 12.2(1): ... Even when the creation of the temporary object is avoided (12.8), all the semantic restrictions must be respected as if the temporary object was created. From C++ 12.2(5): [an example with a class C] the expression C(16)+C(23) creates three temporaries. All the above is related to things I had said before In the thread about references. About temporaries for basic types: The right place to look is the C-standard (C++ is almost identical to C w.r.t. them), but since C objects do not involve Ctors and Dtors, temporaries as such are of a lesser concern compred to C++. They are termed "rvalues" and in the C-Standard terminology: "values of expressions". Here is something from C99 6.8.6.4(3): If a return statement with an expression is executed, the _value_of_the_expression_ [my emph. z.] is returned to the caller as the value of the function call expression. If the expression has a type different from the return type of the function in which it appears, the value is converted as if by assignment to an object having the return type of the function. The C++ standard emphasizes "class type" or "class object" when necessary (and in most discussions about temporaries it does not do that), but here is a line from an example about templates 14.1(6): const int& cri = i; //OK: const reference bound to temporary This explicitly says that temporaries of basic types are possible. About the possibility of eliminating creation of temporaries and even eliminating function invocation: C++ 3.2(2): ... [Note: ... A copy constructor is used even if the call is actually elided by the implementation. ] C++ 12.2(1): ... Even when the creation of the temporary object is avoided (12.8), all the semantic restrictions must be respected as if the temporary object was created. [Example: even if the copy constructor is not called, all the semantic restrictions, such as accessibility (clause 11), shall be satisfied.] An example where a copyCtor can be eliminated is the following: String x("abc"), y = "abc", z = String("abc"); The outcome of the above is the same in all three cases. However, for creation of x a specific Ctor is called, while for creation of z, officially, the same Ctor is called in order to create a temporary, and then copyCtor is called. (y is somewhere in between these two extreme cases). Yet, the compiler is allowed to eliminate the usage of the copyCtorr and create z the same way x is created. Nevertheless, if the copyCtor for String is private, the creation of z will fail with syntax error. A temporary may reside in registers or needs to be stored in memory (see paragraph about number of registers). For example, the case for Person is more involved than that of an int because it requires a Ctor to be called (and, probably, dynamic allocation) and in many cases only parts of the object can reside in registers at the same time. One should not confuse between values that are stored in registers (and may represent lvalue-objects that are being manipulated) and temporaries that represent an intermediate value that is part of a larger process of expression-evaluation, and its existence (or non-existence) should not affect the final outcome of the operation. Temporaries for basic types behave (sometimes) differently than class-objects only because they are (usually) smaller and have only simple mechanisms for creation/destruction.
מקוה שעוזר ומבהיר תכנתו בזהירות