I have:
enum class orientation {
North,
East,
South,
West
};
I want to rotate its instance left (North => West) and right (West => North).
But I don't want to convert them to numbers, because it harms readability and intention and also jumping from the last number to first and back is strange.
I came up with lots of solutions, but all are kind of lame :(
you can iterate the elements like: for(int i=Bar; i<=Last; i++) { ... } Note that this exposes the really-just-an-int nature of a C enum. In particular, you can see that a C enum doesn't really provide type safety, as you can use an int in place of an enum value and vice versa.
A typedef is a mechanism for declaring an alternative name for a type. An enumerated type is an integer type with an associated set of symbolic constants representing the valid values of that type.
Can you loop through an enum C ++? Yes. It iterates over an std::initializer_list<Item>.
Since they're in order:
constexpr auto rotate(orientation o, int n) -> orientation {
// convert to int
int dir = (int)o;
// rotate as an int
dir = (dir + n) % 4;
// account for negatives
if (dir < 0) {
dir += 4;
}
// and then convert back
return orientation{dir};
}
Which you can check:
static_assert(rotate(orientation::North, 1) == orientation::East);
static_assert(rotate(orientation::North, -1) == orientation::West);
I picked the integer to mean "number of 90 degree turns right" but you can adjust as suitable for your actual problem. Or add helper functions like:
constexpr auto rotate_left(orientation o) -> orientation {
return rotate(o, -1);
}
constexpr auto rotate_right(orientation o) -> orientation {
return rotate(o, 1);
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With