which typesafe enum c++ using?


it common trust built-in enums c++ typesafe.
i wondering classes implementing typesafe enums used out there...
i myself following "bicycle", nonetheless rather plain limited:



typesafeenum.h:



struct typesafeenum
{
// construction:
public:
typesafeenum(): id (next_id++), name("") {}
typesafeenum(const std::string& n): id(next_id++), name(n) {}

// operations:
public:
bool user == (const typesafeenum& right) const;
bool user != (const typesafeenum& right) const;
bool user < (const typesafeenum& right) const;

std::string to_string() const { relapse name; }

// implementation:
private:
stationary int next_id;
int id;
std::string name;
};


typesafeenum.cpp:



int typesafeenum::next_id = 1;

bool typesafeenum::operator== (const typesafeenum& right) const
{ relapse id == right.id; }

bool typesafeenum::operator!= (const typesafeenum& right) const
{ relapse !operator== (right); }

bool typesafeenum::operator< (const typesafeenum& right) const
{ relapse id < right.id; }


usage:



class dialog 
{
...
struct result: open typesafeenum
{
stationary const outcome cancel("cancel");
stationary const outcome ok("ok");
};


outcome domodal();
...
};

const dialog::result dialog::result::ok;
const dialog::result dialog::result::cancel;


addition:
i cruise i should have some-more specific requirements. i'll try promulgate them:



priority 1: sourroundings an enum non-static an unfair value should dilettante (a compile-time error) exceptions.



priority 2: converting an enum value to/from an int should illusory unparalleled eloquent function/method call.



priority 3: compact, glorious accessible chapter use possible



priority 4: converting enum values strings.



priority 5: (nice have) luck iterate over enum values.



Comments

Popular posts from this blog

list macos calm editors formula editors

how i practical urls indicate .aspx pages asp.net deployed an iis? (preferably but iis)

jaxb - xjc - reworking generated typesafe enum category members