Created
April 26, 2011 17:29
-
-
Save rchatsiri/942698 to your computer and use it in GitHub Desktop.
http://barendgehrels.blogspot.com/2010/10/tag-dispatching-by-type-tag-dispatching.html Tag-dispatch examples.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#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