c++ - Intuitive way to make something like a member function for an enum class (C++11) -


near can tell ideal thing i'm looking outside of c++11 spec, if otherwise had enum class core of this:

enum class color { red, orange, yellow, green, cyan, azure, blue, magenta }; 

i ideally have wanted have (non-virtual) member functions such define this:

color color::complement() const {   switch(*this) {     case red:return cyan;     case orange: return azure;     case yellow: return blue;     case green: return magenta;     case cyan: return red;     case azure: return orange;     case blue: return yellow;     case magenta: return green;   } } 

of course, c++11 spec doesn't allow define member functions enum classes, not possible.

now strictly speaking, don't really require complement function regular member function, specifically, if not be, ideally want somehow still specified using color:: scope resolution. static member function have been fine, taking color argument parameter, again... c++11 not allow have static member functions in enum class.

one option had occured me sacrifice preference explicitly specifying color:: in name, , define function @ same level color enum follows:

color complement(color orginal)  {   switch(orginal) {   ...   } } 

the biggest problem have approach complement function not bound class namespace of color, , although argument checking assures won't used in context, feel code utilizes function clearer reader of code if function called via color::complement, in opinion reads more idiomatically complement. further, such encoding of type how 1 calls function enforced compiler (the ideal, in opinion, since not necessitate function name more predicate clear 1 reading code on function's purpose , how used), , name cannot inadvertently deprecated if underlying types should change, biggest problem trying use naming scheme somehow encodes names of datatypes names of identifiers themselves.

i had briefly considered using namespaces, so:

namespace color   enum  { red, orange, yellow, green, cyan, azure, blue, magenta };   int complement(int orginal)   {     switch(original) {      ....     }   } } 

which allow me specify colors color::red, color::blue, etc, able specify color::complement(x) complement of color contained in x, approach not have strong type checking enum class does. if used enum class inside of color namespace itself, forced specify colors color::additional_name::red, etc... instead of color::red, how want able specify them.

c++ not allow me have both enum class called color , namespace color, there no way syntactically achieve after way either.

so... there way achieve scoping like, strong type-checking want, or hosed, , expecting more of language designed do?

it isn't you're asking for, overload operator enum class, , use complement it.

enum class color {    red,   orange,   yellow,   green,   cyan,   azure,   blue,   magenta };  color operator~(color c) {    switch(c) {      case color::red:     return color::cyan;     case color::orange:  return color::azure;     case color::yellow:  return color::blue;     case color::green:   return color::magenta;     case color::cyan:    return color::red;     case color::azure:   return color::orange;     case color::blue:    return color::yellow;     case color::magenta: return color::green;    } }  ~color::red == color::cyan etc... 

Comments

Popular posts from this blog

dns - How To Use Custom Nameserver On Free Cloudflare? -

python - Pygame screen.blit not working -

c# - Web API response xml language -