struct S {
S();
S(int);
friend S operator+(const S&, const S&);
~S();
};
const S& cr = S(16)+S(23);
The temporary result of "S(16) + S(23)" bound to "cr" persists until the end life of "cr".
2) Example
class X {
public:
X(int);
X(const X&);
X& operator=(const X&);
~X();
};
class Y {
public:
Y(int);
Y(Y&&);
~Y();
};
X f(X);
Y g(Y);
void h() {
X a(1);
X b = f(X(2));
Y c = g(Y(3));
a = f(a);
}
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.
Likewise, an implementation might use a temporary in which to construct Y(3) before passing it to g() using Y’s move constructor; alternatively, Y(3) 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.
Likewise, a temporary might be used to hold the result of g(Y(3)) before moving it to c using Y’s move constructor; alternatively, g()’s result might be constructed in c.
On the other hand, the expression a=f(a) requires a temporary for the result of f(a), which is then assigned to a.