I have a function to change the state of an LED that takes in an enum argument with three possible values:
enum class Command { Off, On, Toggle };
void led(Command);
I'd like to be able to use the led() function with bool arguments: led(true) or led(false).
I realize that I can overload the void led(Command) function with a void led(bool) but I feel like this shouldn't be necessary.
Specifically, I'm thinking I should be able to define an operator that provides an explicit conversion from bool to Command. I haven't been able to find the syntax for this. My current best guess is:
inline constexpr operator Command(bool on) { return on ? Command::On : Command::Off; }
But it seems the operator overloads can only be declared inside classes? I can see how I could do this with a full class instead of an enum class, but that seems heavy here.
Is what I want to do possible?
void led(bool)enum classonly does so much. If you want it to have behavior, you may want to useclass Command { public: enum Op { Off, On, Toggle}; /*...*/};and then you have a place to add in member functions, conversion constructors,explicit operator bool, et cetera.