Multiple dispatch using templates - too good to be true?

T

Tigera

Greetings,

I too have succumbed to the perhaps foolish urge to write a video
game, and I have been struggling with the implementation of multiple
dispatch. I read through "More Effective C++" by Scott Meyers, and
though I am impressed with his implementation, I wanted to find a way
to use multiple dispatch in a way that was less complicated.
Forgive me if the result, below, has been posted before or written of
before - I haven't read the "Purple Book" at all, so maybe it's all
been done before. What I'd like to know is what I'm missing - this
compiles on my machine, but am I doing something horribly wrong by
trying this? If I add a class later, like one out of a dynamically-
linked library, am I going to have to recompile everything? It just
seems to be too easy to be true.

//File: base_dispatch.h
#ifndef __BASEDISPATCH__
#define __BASEDISPATCH__

class BaseDispatcher
{
public:
virtual void Dispatch(void* A) = 0;
};
#endif

//File: dispatchany.cpp
#ifndef __DISPATCHANY__
#define __DISPATCHANY__

#include "base_dispatcher.h"

template<class T=BaseDispatcher, class U=BaseDispatcher>
class DispatchAny
{
public:
static void Dispatch(T* dispatcher, void* arg)
{
dispatcher->Dispatch(arg);
}

static void Dispatch(T* dispatcher, U* arg)
{
dispatcher->Dispatch(arg);
}
};


#endif

//File: testdispatchany.cpp
#include "dispatchany.cpp"
#include <iostream>

using std::cout;
using std::endl;

class Attack;
class Ability
{
public:
virtual void Dispatch(void* A) { cout<<"Dispatched to void in
ability"<<endl; }
virtual void Dispatch(Attack* a) { cout<<"Ability dispatches
attack"<<endl; }
};


class Attack
{
public:
virtual void Dispatch(void* A) { cout<<"Dispatched to void in
attack"<<endl; }
virtual void Dispatch(Ability* a) { cout<<"Attack dispatches
ability"<<endl; }
};


class Melee : public Attack
{
public:
virtual void Dispatch(void* A) { cout<<"Dispatched to void in
melee"<<endl; }
virtual void Dispatch(Ability* a) { cout<<"Melee dispatches
ability"<<endl; }
};


int main()
{
Ability* a = new Ability();
Attack* b = new Attack();

int* test = new int(5);

Melee* melee = new Melee();

DispatchAny<Ability, Attack> c;
DispatchAny<Attack,Ability> d;
DispatchAny<Melee,Ability> e;

c.Dispatch(a,b);
d.Dispatch(b,a);
e.Dispatch(melee,a);
d.Dispatch(melee,a);
c.Dispatch(a,a);
c.Dispatch(a,test);

delete a;
delete b;
delete melee;
delete test;

return 0;
}


I seek the wisdom of the opinions of those more experienced than I,
and I thank you for them.
 
R

red floyd

Tigera said:
Greetings,
//File: base_dispatch.h
#ifndef __BASEDISPATCH__
#define __BASEDISPATCH__


//File: dispatchany.cpp
#ifndef __DISPATCHANY__
#define __DISPATCHANY__

[redacted]
I seek the wisdom of the opinions of those more experienced than I,
and I thank you for them.

Do not use two consecutive underscores in your include guards, or in any
other identifier. Any such identifier is reserved to the implementation
(compiler vendor), also, any identifier with a single leading underscore
followed by an upper case letter is so reserved.

Your program is therefore ill-formed.
 
T

Tigera

Tigera said:
Greetings,
//File: base_dispatch.h
#ifndef __BASEDISPATCH__
#define __BASEDISPATCH__
//File: dispatchany.cpp
#ifndef __DISPATCHANY__
#define __DISPATCHANY__
[redacted]
I seek the wisdom of the opinions of those more experienced than I,
and I thank you for them.

Do not use two consecutive underscores in your include guards, or in any
other identifier. Any such identifier is reserved to the implementation
(compiler vendor), also, any identifier with a single leading underscore
followed by an upper case letter is so reserved.

Your program is therefore ill-formed.

Interesting. I've never heard of that before. Like I said, it
compiled and ran OK, though I did change the code as you prescribed.
I've been using include guards with double-underscores for years -
looks like I've been messing that up for a while. Thanks for the
correction. The code still compiles and runs after this, do you see
any other problems?
 
M

Markus Schoder

Tigera said:
Greetings,

I too have succumbed to the perhaps foolish urge to write a video
game, and I have been struggling with the implementation of multiple
dispatch. I read through "More Effective C++" by Scott Meyers, and
though I am impressed with his implementation, I wanted to find a way
to use multiple dispatch in a way that was less complicated.
Forgive me if the result, below, has been posted before or written of
before - I haven't read the "Purple Book" at all, so maybe it's all
been done before. What I'd like to know is what I'm missing - this
compiles on my machine, but am I doing something horribly wrong by
trying this? If I add a class later, like one out of a dynamically-
linked library, am I going to have to recompile everything? It just
seems to be too easy to be true.

//File: base_dispatch.h
#ifndef __BASEDISPATCH__
#define __BASEDISPATCH__

class BaseDispatcher
{
public:
virtual void Dispatch(void* A) = 0;
};
#endif

//File: dispatchany.cpp
#ifndef __DISPATCHANY__
#define __DISPATCHANY__

#include "base_dispatcher.h"

template<class T=BaseDispatcher, class U=BaseDispatcher>
class DispatchAny
{
public:
static void Dispatch(T* dispatcher, void* arg)
{
dispatcher->Dispatch(arg);
}

static void Dispatch(T* dispatcher, U* arg)
{
dispatcher->Dispatch(arg);
}
};


#endif

//File: testdispatchany.cpp
#include "dispatchany.cpp"
#include <iostream>

using std::cout;
using std::endl;

class Attack;
class Ability
{
public:
virtual void Dispatch(void* A) { cout<<"Dispatched to void in
ability"<<endl; }
virtual void Dispatch(Attack* a) { cout<<"Ability dispatches
attack"<<endl; }
};


class Attack
{
public:
virtual void Dispatch(void* A) { cout<<"Dispatched to void in
attack"<<endl; }
virtual void Dispatch(Ability* a) { cout<<"Attack dispatches
ability"<<endl; }
};


class Melee : public Attack
{
public:
virtual void Dispatch(void* A) { cout<<"Dispatched to void in
melee"<<endl; }
virtual void Dispatch(Ability* a) { cout<<"Melee dispatches
ability"<<endl; }
};


int main()
{
Ability* a = new Ability();
Attack* b = new Attack();

int* test = new int(5);

Melee* melee = new Melee();

DispatchAny<Ability, Attack> c;
DispatchAny<Attack,Ability> d;
DispatchAny<Melee,Ability> e;

c.Dispatch(a,b);
d.Dispatch(b,a);
e.Dispatch(melee,a);
d.Dispatch(melee,a);
c.Dispatch(a,a);
c.Dispatch(a,test);

delete a;
delete b;
delete melee;
delete test;

return 0;
}


I seek the wisdom of the opinions of those more experienced than I,
and I thank you for them.

I fail to see where you actually do multiple dispatch. The dispatch
seems to only occur on the first parameter. If you want to, say
dispatch on Attack and Ability the setup would be something like:

struct Ability;

struct Attack
{
virtual void Dispatch(Ability *) = 0;
};

struct Melee;
struct Projectile;

struct Ability
{
virtual void Dispatch(Melee *) = 0;
virtual void Dispatch(Projectile *) = 0;
};

void Dispatch(Attack *attack, Ability *ability)
{
attack->Dispatch(ability);
}

With Melee and Projectile looking something like:

struct Melee : Attack
{
void Dispatch(Ability *ability)
{
ability->Dispatch(this);
}
};

struct Projectile : Attack
{
void Dispatch(Ability *ability)
{
ability->Dispatch(this);
}
};

To avoid repeating the boiler plate the curiously recurring template
pattern can be used:

template<class T>
struct AttackDispatch : Attack
{
void Dispatch(Ability *ability)
{
ability->Dispatch(static_cast<T *>(this));
}
};

struct Melee : AttackDispatch<Melee>
{
};

struct Projectile : AttackDispatch<Projectile>
{
};

This will be even more useful if there are multiple multiple dispatch
functions (no pun intended).

But maybe I am just misunderstanding what you are trying to do.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,994
Messages
2,570,223
Members
46,813
Latest member
lawrwtwinkle111

Latest Threads

Top