Boost logo

Boost :

Subject: Re: [boost] [proto] Problems with operator <<
From: Eric Niebler (eric_at_[hidden])
Date: 2011-08-26 08:21:28


On 8/26/2011 3:32 AM, John Maddock wrote:
>>> BTW I do specify a proto grammar, and only enable the operators I need
>>> on a case by case basis, in this case though I would need to restrict
>>> the grammar so that proto's shift operators are only enabled for RHS
>>> arguments of integer type. Is that possible?
>>
>> Yes. Can you post your grammar?
>
> Pretty basic at present, need to refine those terminals a bit more! :

Yes!

> struct big_number_grammar
> : proto::or_<
> proto::terminal< proto::_ >

You correctly identified your problem with the stream operators.
std::cout matches because of the above line, and it shouldn't.

> , proto::plus< big_number_grammar, big_number_grammar >
> , proto::multiplies< big_number_grammar, big_number_grammar >
> , proto::minus< big_number_grammar, big_number_grammar >
> , proto::divides< big_number_grammar, big_number_grammar >
> , proto::unary_plus< big_number_grammar >
> , proto::negate< big_number_grammar >
> , proto::modulus<big_number_grammar, big_number_grammar>
> , proto::shift_left<big_number_grammar, big_number_grammar>
> , proto::shift_right<big_number_grammar, big_number_grammar>
> >
> {};
>
> I assume I could replace
>
> , proto::shift_right<big_number_grammar, big_number_grammar>
>
> with something like:
>
> , proto::shift_right<big_number_grammar, integer_terminal>
>
> Where integer_terminal is a big list of terminal integer types, but is
> there any way to use a predicate to say "any type that matches predicate
> X is a terminal"?

Yes. You want:

  // Match any integral terminal
  proto::and_<
    proto::terminal< proto::_ >,
    proto::if_ < boost::is_integral< proto::_value >() >
>

HTH,

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

Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk