In C++ temporaries are unnamed objects that compiler creates in various contexts. The typical uses include reference initialization, argument passing, evaluation of expressions (including standard type conversions), function returns, and exceptions (throw expressions).
As from this link:
When a temporary object is created to initialize a reference variable, the name of the temporary object has the same scope as that of the reference variable. When a temporary object is created during the evaluation of a full-expression (an expression that is not a subexpression of another expression), it is destroyed as the last step in its evaluation that lexically contains the point where it was created.
There are exceptions in the destruction of full-expressions:
- The expression appears as an initializer for a declaration defining an object: the temporary object is destroyed when the initialization is complete.
- A reference is bound to a temporary object: the temporary object is destroyed at the end of the reference's lifetime.
If a temporary object is created for a class with constructors, the compiler calls the appropriate (matching) constructor to create the temporary object.
When a temporary object is destroyed and a destructor exists, the compiler calls the destructor to destroy the temporary object. When you exit from the scope in which the temporary object was created, it is destroyed. If a reference is bound to a temporary object, the temporary object is destroyed when the reference passes out of scope unless it is destroyed earlier by a break in the flow of control. For example, a temporary object created by a constructor initializer for a reference member is destroyed on leaving the constructor.
In cases where such temporary objects are redundant, the compiler does not construct them, in order to create more efficient optimized code. This behavior could be a consideration when you are debugging your programs, especially for memory problems.
Let us summarize it this way. These temporary objects can be created for the following reasons:
To initialize a const reference with an initializer of a type different from that of the underlying type of the reference being initialized.
To store the return value of a function that returns a user-defined type. These temporaries are created only if your program does not copy the return value to an object. Because the return value is not copied to another object, a temporary object is created. A more common case where temporaries are created is during the evaluation of an expression where overloaded operator functions must be called. These overloaded operator functions return a user-defined type that often is not copied to another object.
To store the result of a cast to a user-defined type. When an object of a given type is explicitly converted to a user-defined type, that new object is constructed as a temporary object.
Let's consider the example:
class X {
/ / ...
public:
/ / ...
X(int);
X(const X&);
~X();
};
X f(X);
void g()
{
X a(1);
X b = f(X(2));
a = f(a);
}
Here, an implementation might use a temporary in which to construct X(2)
before passing it to f()
using X
’s copy-constructor; alternatively, X(2)
might be constructed in the space used to hold the argument. Also, a temporary might be used to hold the result of f(X(2))
before copying it to b using X
’s copy-constructor; alternatively, f()
’s result might be constructed in b
. On the other hand, the expression a=f(a)
requires a temporary for the result of f(a)
, which is then assigned to a
.