Skip to content

Instantly share code, notes, and snippets.

@ArGxento
Created October 21, 2012 02:10
Show Gist options
  • Save ArGxento/3925460 to your computer and use it in GitHub Desktop.
Save ArGxento/3925460 to your computer and use it in GitHub Desktop.
enum idiom for non-c++11 environment
#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