Not sure if there is a term for this, it seems that "choice" works. I work in C ++, and I have many unions that I need to create, where the union is the choice of one of the union members. The current "selection" is tracked and always available. I am currently coding these “unions” manually, but I wonder if there is any neat trick to do this kind of (semi) automatically.
I came across a union limitation of the absence of operator overloads or constructor constructors or copy constructors in my first attempt to try to implement this, but realized that since I actually track the current “choice”, there is a very specific behavior that is required in almost every situation .
Here is what I am doing right now (only for two options it can be up to 10 or 15), and this is quite significant code, almost all of which is just a template. Also, if anyone has comments about whether what I really have, really, that would be awesome, still raising some C ++ craziness ...
struct MyChoice { struct Choice1 { int a; char* b; }; struct Choice2 { bool c; double d; }; enum Choice { Choice_Choice1, Choice_Choice2 } choice; char _value[max(sizeof(Choice1),sizeof(Choice2))];
Thanks for your help SO
source share