Re: Multimethods idioms and library support
On Feb 22, 7:02 pm, Larry Evans <cppljev...@suddenlink.net> wrote:
On 02/22/11 10:32, itaj sherman wrote:
[snip]
I'm sorry what I say here is not 100% formally clear. It's more like a
brainstorm to evaluate idioms for multimethods.
itaj
Hi ita,
If g++ is available, you can use the -std=gnu++0x option to enable
variadic templates and then use the code demonstrated the
test driver here:
http://svn.boost.org/svn/boost/sandbox/variadic_templates
/libs/composite_storage/sandbox/pack
/one_of_multiple_dispatch.test.cpp
to do multimethods. Note that that demo uses 2 different methods.
One uses a disjoint union data type (the one with the one_of_maybe tag)
the other uses virtual methods (which is the one you're interested in).
To select the virtual method version, include:
#define REIFIER_VISITOR
in the driver.
So, after I managed to check out the code, it makes more sense.
But, tell me if the following is correct:
using this library on a hirarchy of classes and a certain multimethod.
- I have to collect the declarations of all different overrides in the
same functor class, like functor3 and functor_any do.
- I have to collect all the concrete classes of the hirarchy in one
list, as under hosts_concrete<>
If so, I think this API doesn't solve the main disadvantages I see
with the dynamic_cast idiom.
In my example it would look about:
* except I'm not sure what should be ResultType in there, becuase
hunt() returns void, but what happens if other multimethods return
other types?
struct Carnivours_concrete
: mpl::package< Lion, Anaconda, Bear >
{
};
class Carnivour
{
typedef reifier_visit_abstract_seq< ResultType
, typename Carnivours_concrete::type>
visitor_abstract;
virtual ResultType accept( visitor_abstract const&)const=0;
};
class Lion: public Carnivour
{
...
ResultType accept( visitor_abstract const& a_visitor)const
{
return a_visitor.visit(*this);
}
};
class hunt_functor
{
void operator()( Lion const&, Gazelle& )
{
//jumps on it and bite its neck
}
//and same for:
void operator()( Lion const&, Girrafe& )
{
//bite its ass
}
void operator()( Anaconda const&, Gazelle& )
{
//inject venom
}
//Anaconda can't kill girrafes so no override for that one
void operator()( Bear const&, Prey& )
{
//because Bears catch everything in the same way lol.
}
};
itaj