This is the mail archive of the
mailing list for the GCC project.
Re: Lifetime bug in temporaries
- To: egcs-bugs at cygnus dot com
- Subject: Re: Lifetime bug in temporaries
- From: Corey Kosak <kosak at cs dot cmu dot edu>
- Date: Wed, 25 Mar 1998 02:31:16 -0500
- cc: Marc dot Espie at liafa1 dot liafa dot jussieu dot fr
> Am I missing something, or is g++ lifetime of temporaries still not in
> tune with what it should be ?
I think choice (A). The error in your code appears to be here:
template<class U, class V>inline Expr<SumExpr<Vector<U>, Vector<V> > >
operator+(const Vector<U> &a, const Vector<V> &b)
return make_expr(make_sum(a, b));
the call to make_sum gives you a temporary that's going to disappear real soon
(i.e. at the semicolon), but unfortunately make_expr keeps a reference to it
anyway (stashing it away by passing it to the constructor for Expr.
Indeed, if you make the single-character edit from
template<class U>class Expr
const U &a;
const U a; // let's keep a copy rather than a reference
this appears to fix your immediate problem.