better-enums/example/3-switch.cc
Anton Bachin e784f0c860 Made enum class (strict) conversion opt-in on a global basis.
This makes C++98 and C++11 Better Enums fully compatible by default. If the user
defines BETTER_ENUMS_FORCE_STRICT_CONVERSION before including enum.h, it is
necessary to prefix enum constants in switch cases with '+', but Better Enums
are not implicitly convertible to integers.
2015-05-27 09:02:27 -05:00

39 lines
810 B
C++

// Switch case exhaustiveness checking.
#include <iostream>
#include <enum.h>
ENUM(Channel, int, Red, Green, Blue)
void respond_to_channel(Channel channel)
{
// Try adding an extra case or removing one. Your compiler should issue a
// warning.
switch (channel) {
case Channel::Red:
std::cout << "red channel" << std::endl;
break;
case Channel::Green:
std::cout << "green channel" << std::endl;
break;
case Channel::Blue:
std::cout << "blue channel" << std::endl;
break;
// A redundant case.
// case 3:
// break;
}
}
int main()
{
respond_to_channel(Channel::Red);
respond_to_channel(Channel::Blue);
respond_to_channel(Channel::Green);
return 0;
}