Boost logo

Boost Users :

Subject: Re: [Boost-users] [proto] another const issue
From: Eric Niebler (eric_at_[hidden])
Date: 2009-08-25 15:17:16


Maurizio Vitale wrote:
> Eric, this may or may not be related to the other problem w/ the
> assignment operator.
>
> In my application eventually I'll want to allow literals of builtin
> constants on the right hand side of the assignment, but not on the left
> hand side. So given:
>
> my_int my_i; // a proto terminal
> int i;
>
> The following should happen:
>
> my_i = 5; // OK
> (i,my_i) = my_i; // OK
> (5,my_i) = my_i; // NOT OK
>
> For that I have a grammar for the left hand side and one for the right
> hand side. In the attached file, the grammar for the left hand side
> doesn't match and I'm not sure why.
> What I'm doing wrong?

Here is your grammar:

struct my_lhs_grammar : proto::or_<
   proto::terminal< int& >,
   proto::binary_expr<_, my_grammar, my_grammar>
>
{
};

This says, only terminals that are references to ints are allowed.
my_int is an int terminal, not an int& terminal, therefore it doesn't match.

If you change the grammar to terminal<int>, then it becomes too loose,
because terminal<int> matches int, int& and int const&, as documented.
This wiggle room is useful in most situations, but not in yours. What
you want is this:

struct my_lhs_grammar : proto::or_<
   proto::terminal< int& >,
   proto::terminal< proto::exact<int> >,
   proto::binary_expr<_, my_grammar, my_grammar>
>
{
};

This is all covered in the docs here:
http://www.boost.org/doc/libs/1_39_0/doc/html/proto/users_guide.html#boost_proto.users_guide.intermediate_form.expression_introspection.fuzzy_and_exact_matches_of_terminals

HTH,

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

Boost-users 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