Boost logo

Boost :

From: Eric Niebler (eric_at_[hidden])
Date: 2004-07-23 15:02:53


Rozental, Gennadiy wrote:
>
> BTW, while we are on topic. Your implementation uses code like this
>
> struct any_t {...};
> template<typename T> struct static_any_t : any_t {...}
>
> template<typename T> static_any_t<T> foo(...);
>
> If( any_t const& ref = foo() ) {
> // here you are using ref
> }
>
> Is it indeed allowed in standard and ref is valid reference to temp?
>
> Gennadiy.

Yup, that's perfectly OK. Binding a temporary to a const reference
extends the lifetime of the temporary. And if it's a const reference to
a base, the whole derived object sticks around, not just the base
sub-object (no slicing). In addition, if the derived object has a
non-trivial destructor, it will get called when the const reference to
base goes out of scope /even if base has a non-virtual destructor/. It's
like magic. But not really. :)

The real magic in BOOST_FOREACH is how to recover the lost type
information without virtuals, dynamic casts or RTTI. And that's with
sneaky template tricks and static_cast.

-- 
Eric Niebler
Boost Consulting
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