Created
October 21, 2012 02:10
-
-
Save ArGxento/3925460 to your computer and use it in GitHub Desktop.
enum idiom for non-c++11 environment
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
#include<iostream> | |
#include<boost/variant.hpp> | |
namespace E1 { | |
enum Type { | |
a,b,c | |
}; | |
struct stringize:std::string { | |
stringize(Type e) { | |
switch(e) { | |
#define ENUM_STRINGIZE(elem) break;case elem:assign(#elem) | |
ENUM_STRINGIZE(E1::a); | |
ENUM_STRINGIZE(E1::b); | |
ENUM_STRINGIZE(E1::c); | |
#undef ENUM_STRINGIZE | |
break;default:assign("Undef"); | |
} | |
} | |
}; | |
} | |
namespace E2{ | |
enum Type { | |
a,b,c | |
}; | |
struct stringize: std::string{ | |
stringize(Type e){ | |
switch(e){ | |
#define ENUM_STRINGIZE(elem) break;case elem:assign(#elem) | |
ENUM_STRINGIZE(E2::a); | |
ENUM_STRINGIZE(E2::b); | |
ENUM_STRINGIZE(E2::c); | |
#undef ENUM_STRINGIZE | |
break;default:assign("Undef"); | |
} | |
} | |
}; | |
} | |
int main(){ | |
boost::variant<E1::Type, E2::Type, int> v = E2::b; | |
std::cout << | |
v.which() << | |
E2::stringize(boost::get<E2::Type>(v))<< | |
boost::get<int>(v) | |
; | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment