> The standard says that the temporary should live long enough for the
expression enclosing it to finish, but I wouldn't necessarily count on
this behavior unless you are sure that your particular compiler gets
it right.
Okay. Here is another example:
template <class First, class Second>
class Plus
{
public:
Plus( const First & first, const Second & second ) : first( first ),
second( second ) { }
template <class Third>
Plus< Plus<First, Second>, Third > operator+( const Third & third ) { return
Plus<Plus<First,Second>, Third>( *this, third ); }
const First & first;
const Second & second;
};
class Matrix
{
public:
Plus<Matrix, Matrix> operator+( const Matrix & m ) const { return
Plus<Matrix, Matrix>( *this, m ); }
template <class Type>
Matrix & operator=( const Type & type ) { /* whatever */ return *this; }
};
void test()
{
Matrix a, b, c, d;
a = b + c + d; // [1]
}
Will the temporaries created at [1] live until the whole expression is
finished (ie. until the assignment to 'a' completes)? As far as I know, the
code should be legal since all temporaries created during an evaluation of
an expression (statement) must live until the end of the statement. In fact,
all expression templates based linear algebra labraries rely on this
behaviour.
Thanks
-- Marek