Boost logo

Proto :

Subject: Re: [proto] Held nodes by value for Fundamental types
From: Fernando Pelliccioni (fpelliccioni_at_[hidden])
Date: 2012-04-10 09:48:11


Hello,

On Mon, Apr 9, 2012 at 7:00 PM, Eric Niebler <eric_at_[hidden]> wrote:

> On 4/9/2012 2:21 PM, Fernando Pelliccioni wrote:
> > Hello,
> >
> > I'm wondering if it would be appropriate to treat the fundamental types
> > (char, short, int, double, ...) by value, by default.
> >
> > I wrote this simple piece of code.
> > I'm not sure if I'm leaving without considering any other implication,
> > but I think it may be an improvement.
> > Please, tell me if I am wrong.
>
> Thanks. I thought long about whether to handle the fundamental types
> differently than user-defined types and decided against it. The
> capture-everything-by-reference-by-default model is easy to explain and
> reason about. Special cases can be handled on a per-domain basis as needed.
>
>
Thanks for your reply.
The design philosophy is unobjectionable.

> There is a way to change the capture behavior for your domain. The newly
> released version of Proto documents how to do this (although the
> functionality has been there for a few releases already).
>
>
> http://www.boost.org/doc/libs/1_49_0/doc/html/proto/users_guide.html#boost_proto.users_guide.front_end.customizing_expressions_in_your_domain.per_domain_as_child
>
>
I had read that chapter, but I thought it might be the behavior of
default_domain.

> In short, you'll need to define an as_child metafunction in your domain
> definition:
>
> class my_domain
> : proto::domain< my_generator, my_grammar >
> {
> // Here is where you define how Proto should handle
> // sub-expressions that are about to be glommed into
> // a larger expression.
> template< typename T >
> struct as_child
> {
> typedef unspecified-Proto-expr-type result_type;
>
> result_type operator()( T & t ) const
> {
> return unspecified-Proto-expr-object;
> }
> };
> };
>
> In as_child, you'll have to do this (pseudocode):
>
> if (is_expr<T>)
> return T &
> else if(is_fundamental<T>)
> return proto::terminal<T>::type
> else
> return proto::terminal<T &>::type
>
>
Done!
I implemented it by defining as_child in my domain.

> The metaprogramming is left as an exercise. :-)
>
>
... Happy metaprogramming :)

Thanks for your clarification.
Fernando.



Proto list run by eric at boostpro.com