Created
September 11, 2012 22:09
-
-
Save satoruhiga/3702499 to your computer and use it in GitHub Desktop.
Type2String
This file contains hidden or 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
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