Skip to content

Instantly share code, notes, and snippets.

@satoruhiga
Created September 11, 2012 22:09
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save satoruhiga/3702499 to your computer and use it in GitHub Desktop.
Save satoruhiga/3702499 to your computer and use it in GitHub Desktop.
Type2String
template<typename T>
struct Type2String
{
static std::string value()
{
throw std::runtime_error("unregistered type");
}
};
#define REGISTER_TYPE(t) \
template<> \
struct Type2String<t> \
{ \
static std::string value() \
{ \
return #t; \
} \
};
#define REGISTER_TYPE_WITH_NAME(t, n) \
template<> \
struct Type2String<t> \
{ \
static std::string value() \
{ \
return n; \
} \
};
REGISTER_TYPE_WITH_NAME(int, "int");
int main()
{
cout << Type2String<int>::value() << endl;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment