Boost logo

Boost :

Subject: Re: [boost] [proto] proto::as_child potentially unsafe?
From: Eric Niebler (eric_at_[hidden])
Date: 2011-09-09 13:25:56


On 9/9/2011 12:47 PM, John Maddock wrote:
> Following the guide here:
> http://www.boost.org/doc/libs/1_47_0/doc/html/proto/users_guide.html#boost_proto.users_guide.front_end.making_lazy_functions.lazy_functions_made_simple_with__literal_make_expr____literal_
>
>
> I fell into a nasty trap: as_child stores it's argument as a reference,
> and as far as I can tell there is no way to force it to store by value,
> unless you apply deep_copy to the whole expression tree (not just the
> one child created with as_child).
>
> I found this when trying to protoize a function that looked like:
>
> template <class Exp>
> some-return-type foo(const Exp&, int i);
>
> In which the value "i" was returned as a reference to a value on the
> stack which then went out of scope :-(
>
> Even if the function has a signature such as:
>
> template <class Exp>
> some-return-type foo(const Exp&, const int& i);
>
> There is still a potential trap, because you don't know where "i" came
> from and when it will go out of scope - OK so the result of the
> expression will *usually* be calculated before that happens - but you
> never know.
>
> IMO I think this trap needs better documenting, because the section in
> question (and text preceding it) read to me "as if" arguments to
> as_child were stored by value.
>
> I'm sure I'm reading that wrong.... but you know ;-)

You're not reading it wrong. proto::as_expr exists for when you want
things stored by value.

-- 
Eric Niebler
BoostPro Computing
http://www.boostpro.com

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