Boost logo

Proto :

Subject: Re: [proto] Held nodes by value for Fundamental types
From: Eric Niebler (eric_at_[hidden])
Date: 2012-04-09 18:00:09

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.

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

In short, you'll need to define an as_child metafunction in your domain

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
  return proto::terminal<T &>::type

The metaprogramming is left as an exercise. :-)

Eric Niebler
BoostPro Computing

Proto list run by eric at