Boost logo

Boost Users :

From: Joel de Guzman (djowel_at_[hidden])
Date: 2003-05-11 21:17:51


Rainer Deyke wrote:
> Slava Akhmechet wrote:
>> I was able to get boost::bind working to bind the semantic action to
>> the member function of MyGrammar. This seems like a superior
>> solution. In any case, I am not sure how to pass custom data to a
>> functor. When specifing a semantic action I have to pass the type of
>> the functor, which implies that spirit creates a temporary object. Am
>> I missing something?
>
> No, you pass an instance of the functor, which can be cosntructed with
> arbitrary arguments.
>
> struct functor {
> f(char const *s) : msg(s) {}
> template<class Iter> void operator(Iter, Iter)
> {
> std::cout << this->msg;
> }
> char const *msg;
> };
>
> parse(something, something_p[functor("something")]);

The preferred way is to keep the semantic functors as light as possible.
As Rainer suggested, a pointer is ok. As for me, I prefer keeping a
reference to some external data. Example:

struct functor {
   f(T& data) : data(data) {}
   template<class Iter> void operator(Iter, Iter);
   T& data;
};

BTW, I suggest continuing this thread here:

Spirit-general mailing list
Spirit-general_at_[hidden]
https://lists.sourceforge.net/lists/listinfo/spirit-general

-- 
Joel de Guzman
joel at boost-consulting.com
http://www.boost-consulting.com
http://spirit.sf.net

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