wise_enum
wise_enum copied to clipboard
Switch case "lifts"
The README mentions it is TODO.
What's left to do and do you want any help implementing this? Note that mp11 has mp_with_index which does most of the work.
Hey sorry it took me a while to respond. Implementation is straightforward, I would just generate it with the macros similar to the to_stuff. I actually considered implementing the to_string in terms of this construct but I think across different compilers I wasn't 100% convinced the assembly would be the same.
The real question is deciding on the interface. Most notably, what should happen when the enum passed in isn't actually any of the enumeration values (which is legal). One possibility is to have the user pass in two lambdas with the second taking no arguments and getting called in the case where no enum is found.
Any thoughts?
Sorry didn't notice your reply.
I would throw an exception. Out of range values should be exceptional so that wouldn't turn into misusing exception for control flow. I would consider this the most intuitive interface, because it is the simplest and follows what std::visit does. I would also add a function bool wise_enum::out_of_range<E>(v)
(analogous to valueless_by_exception). That would let users check prior to the lift whether it'll succeed and avoid the possibility of an exception (e.g. if they compile with exceptions disabled).