Boost logo

Boost :

Subject: [boost] [proto] proto::as_child potentially unsafe?
From: John Maddock (boost.regex_at_[hidden])
Date: 2011-09-09 12:47:10

Following the guide here:

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 ;-)


Boost list run by bdawes at, gregod at, cpdaniel at, john at