
Boost Users : 
Subject: Re: [Boostusers] [Proto] Functions returning an expression
From: Eric Niebler (eric_at_[hidden])
Date: 20081114 15:10:29
Joel Falcou wrote:
> Eric Niebler a écrit :
>> Why don't you want to use make_expr? That's what it's for. You build
>> the return type with proto::result_of::make_expr, and call
>> proto::make_expr() from with f().
>>
> I have nothing against make_expr for building a single node of the
> expression.
> I have tons of such function combining long chains of call and i don't
> thin chaing large amount of make_expr is good.
>
> Is there a way to use BOOST_TYPEOF with proto ?
> So maybe i can write :
>
> BOOST_TYPEOF( (vec_expr<A>()+vec_expr<B>())/(vec_expr<A>()vec_expr<B>())
> f( ... ) {}
You could use BOOST_TYPEOF, but you need to be careful about lifetime
management. The resulting expression will have references to temporary
objects, and returning it without deepcopying it first will cause
dangling references.
You could get some mileage from the operator metafunctions. Eg:
template<typename A, typename B>
vec_expr<
typename proto::divides<
vec_expr<
typename proto::plus<
vec_expr<A> const &,
vec_expr<B> const &
>::type
>,
vec_expr<
typename proto::minus<
vec_expr<A> const &,
vec_expr<B> const &
>::type
>
>::type
> const
f(vec_expr<A> const &a, vec_expr<B> const &b)
{
vec_expr<
typename proto::divides<
vec_expr<
typename proto::plus<
vec_expr<A> const &,
vec_expr<B> const &
>::type
>,
vec_expr<
typename proto::minus<
vec_expr<A> const &,
vec_expr<B> const &
>::type
>
>::type
> that = {{{{a,b}},{{a,b}}}}; // assumes vec_expr uses
// BOOST_PROTO_EXTENDS
return that;
}
This way you can be very explicit about what nodes are stored by
reference and which are by value. I don't see this as better than
make_expr, though, which is IMO your best bet.
 Eric Niebler BoostPro Computing http://www.boostpro.com
Boostusers list run by williamkempf at hotmail.com, kalb at libertysoft.com, bjorn.karlsson at readsoft.com, gregod at cs.rpi.edu, wekempf at cox.net