Boost logo

Boost Users :

Subject: Re: [Boost-users] [proto] defining function call operators on expressions
From: Eric Niebler (eric_at_[hidden])
Date: 2010-02-28 04:28:45


On 2/28/2010 7:17 PM, Manjunath Kudlur wrote:
> I am trying to define operator()(...) for my expressions. I want to be
> able to form expression of the form Program_(a,b) and call Program_(a,
> b)(10,20). Here, 'a' and 'b' are terminals and I have a mechanism
> shown in the code below that somehow gets the type suitable for the
> parameter position. Suppose 'a' is terminal<Var<int> >, I want the
> first parameter position in Program_(a,b) (pos1, pos2) to be of the
> type 'int'.
<snip>

OK.

> template<int N>
> struct call_param_type
> : proto::or_<
> proto::when<
> proto::function<
> proto::terminal<program_>,
> proto::vararg<proto::terminal<Var<proto::_> > > >,
> var_type(proto::_child_c<N+1>)>
> > {};

OK, the call_param_type grammar only matches expressions of the type
Program_(a, b).

> template<typename Expr>
> struct program_expr {
> BOOST_PROTO_BASIC_EXTENDS(Expr, program_expr<Expr>, program_domain);
> BOOST_PROTO_EXTENDS_SUBSCRIPT();
>
> typedef void result_type;
>
> #if 0
> result_type operator()(typename
> boost::result_of<call_param_type<0>(const Expr&)>::type x) {
> std::cout<< "program with one arg\n";
> }
<snip>

This function definition eagerly evaluates the call_param_type grammar's
transform against Expr regardless of whether Expr matches
call_param_type's grammar or not. Trouble. Look at how you define the
Program_ function:

> template<typename A0>
> typename proto::result_of::make_expr<proto::tag::function
> , program_domain
> , program_
> , A0 const &>::type const
> Program_(A0 const &a0)

This creates an expression node with tag type "tag::function" and two
children: a program_ terminal and A0. When make_expr turns program_ into
a Proto terminal, it uses program_domain to wrap the terminal in a
program_expr. That's bad news because a program_ terminal doesn't match
the call_param_type grammar. You're violating the transform's precondition.

Suggestion: when you are having problems with evaluating a transform,
try adding an MPL assertion that the expression you're passing to it
actually matches the grammar.

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