Good day. The situation is the following, a packet comes from the server, in a certain field of which there is a byte that I want to transfer to the method as a member of a certain enum. To cast, I use a static caste, but I wondered what would happen if the server sends me incorrect data and the value in this byte is outside of enum. I threw in the test code and, as it turned out, C ++ calmly swallows it and there is nothing to throw (.
enum class MyEnum { Red = 1, Black = 2, White = 3 }; void Foo(MyEnum val) { std::cout << static_cast<int>(val) << std::endl; }; int main() { Foo(MyEnum::Red); Foo(static_cast<MyEnum>(3)); Foo(static_cast<MyEnum>(5)); system("PAUSE"); return 0; } Are there any standard tools to effectively (in terms of performance) check the entry of a value into a specific enum and give me the opportunity to throw an exception. If they are not, then how to do it, not shoveling the entire enum with his hands. And is it possible to do something like foreach enuma bypassing a garden (something like for (auto i: MyEnum) ...)?
ps C ++ 14 compiler GCC 6.3.