Boost logo

Boost :

From: David Abrahams (dave_at_[hidden])
Date: 2002-10-13 12:29:14


Daniel Frey <daniel.frey_at_[hidden]> writes:

> David Abrahams wrote:
> >
> > Daniel Frey <daniel.frey_at_[hidden]> writes:
> >
> > A const& is not a transparent replacement for a value in other
> > ways. Consider:
> >
> > const iterator& x = c.end();
> > c.insert(start, finish);
> > // is x valid here, or dangling?
>
> 'const iterator x = c.end();' won't be any better, would it? Maybe I
> need some more explainations here :))

The question has to do with whether a const& _return_ _value_ is a
transparent replacement for a by-value return. If c.end() returns a
value, x binds to the temporary and keeps it alive for the duration of
the scope. If c.end() returns a const reference, mutating functions on
c may invalidate or change the object to which x is bound.

HTH,

-- 
           David Abrahams * Boost Consulting
dave_at_[hidden] * http://www.boost-consulting.com

Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk