[proto] another const issue
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? TIA, Maurizio
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_p... HTH, -- Eric Niebler BoostPro Computing http://www.boostpro.com
"Eric" == Eric Niebler
writes:
Eric> Maurizio Vitale wrote: Eric> This says, only terminals that are references to ints are Eric> allowed. my_int is an int terminal, not an int& terminal, Eric> therefore it doesn't match. Ops. I still have in the back of my mind that everything is by-reference in proto, so saying int& was my way to eliminate the const int& case. Thanks. Maurizio
Maurizio Vitale wrote:
"Eric" == Eric Niebler
writes: Eric> Maurizio Vitale wrote:
Eric> This says, only terminals that are references to ints are Eric> allowed. my_int is an int terminal, not an int& terminal, Eric> therefore it doesn't match.
Ops. I still have in the back of my mind that everything is by-reference in proto, so saying int& was my way to eliminate the const int& case.
Thanks.
Proto will hold things by reference or by value, depending on what you ask for. You defined the my_int terminal type as: struct my_int : my_expr< proto::terminal< int >::type > That's an int held by value. -- Eric Niebler BoostPro Computing http://www.boostpro.com
participants (2)
-
Eric Niebler
-
Maurizio Vitale