Skip to content

Instantly share code, notes, and snippets.

@rchatsiri
Created April 26, 2011 17:29
Show Gist options
  • Save rchatsiri/942698 to your computer and use it in GitHub Desktop.
Save rchatsiri/942698 to your computer and use it in GitHub Desktop.
#ifndef TAG_DISPATCH_H
#define TAG_DISPATCH_H
struct apple_tag{};
struct banana_tag{};
struct orange_tag{};
struct apple
{
double reduis;
std::string name;
apple(std::string const& n): name(n){}
};
struct banana
{
double length;
std::string name;
banana(std::string const& n): name(n){}
};
namespace dispatch{
template <typename Tag> struct eat{};
template<>struct eat<apple_tag>
{
static void apply(apple const& a){
std::cout<<"Apple tag"<<std::endl;
}
};
/* void eat(apple const& a, apple_tag){
std::cout<<"Apple tag"<<std::endl;
} */
template<>struct eat<banana_tag>
{
static void apply(banana const& b){
std::cout<<"Banana tag"<<std::endl;
}
};
/* void eat(banana const& b, banana_tag){
std::cout<<"Banana tag"<<std::endl;
} */
}
/* template<typename T>
void eat(T const& friut){
typename tag<T>::type the_tag;
dispatch::eat( friut, the_tag);
} */
template <typename T>
void eat(T const& fruit)
{
dispatch::eat<typename tag<T>::type>::apply(fruit);
}
#endif
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment