Boost logo

Boost :

From: Tobias Schwinger (tschwinger_at_[hidden])
Date: 2006-10-26 17:19:14


Aristid Breitkreuz wrote:
>>[snip]
>>Well, the experiment was driven by the question whether it would be
>>possible to add more features of spirit::rule to a macro-based
>>replacement that uses typeof instead of virtual dispatch (see
>>boost/spirit/utility/rule_parser.hpp, note: no named parameters in
>>there, yet) without compromising the interface or even making it
>>better.
>
> Oh. Virtual dispatch is still necessary for recursion, right?

No.

Of course you'll need a static call to recurse and a dynamic one to return at /some/ point (since the compiler can't inline forever), but you do not need virtual dispatch, which involves at least two dynamic calls (including the return) plus the vtable lookup.

It works for the same reason CRTP does -- the laziness of template instantiation makes it possible. Here is some code that attempts to illustrate how:

   template<class X>
   class A
   {
       X & ref_that;
   public:

       explicit A(X & that)
         : ref_that(that)
       { }

       void do_()
       {
           ref_that.do_();
       }
   };

   class B
   {
       A<B> obj_buddy;
   public:

       B()
         : obj_buddy(*this)
       { }

       void do_()
       {
           obj_buddy.do_();
       }
   };

'A<B>::do_' and 'B::do_' recurse back and forth.

Spirit's subrule template also works without virtual dispatch (however, the setup happens expression level).

Regards,

Tobias


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