I would suggest, since we are talking about speed and a small number of commands - use just table transformations. And the table will not be excessively large.
Sample code ( link to ideone ):
#include <iostream> enum class BoardType : uint8_t { Classic = 0, Static = 1, Smart = 2, Bad = 255 }; BoardType Cast[UINT8_MAX]; // <- сюда просто забить константы -++ // inline BoardType ToEnum(const uint8_t i) { // BoardType Ret = Cast[i]; // if (Ret == BoardType::Bad) throw std::range_error("Беда"); // return Ret; // } // // int main () { // ////// вместо инициализации ниже - /////////////////////////////// for(auto i=0; i<UINT8_MAX; i++) Cast[i] = BoardType::Bad; Cast[0] = BoardType::Classic; Cast[1] = BoardType::Static; Cast[2] = BoardType::Smart; ////////////////////////////////////////////////////////////////// try { std::cout << static_cast<int>(ToEnum(0)) << std::endl; std::cout << static_cast<int>(ToEnum(1)) << std::endl; std::cout << static_cast<int>(ToEnum(2)) << std::endl; std::cout << static_cast<int>(ToEnum(3)) << std::endl; } catch(std::range_error &e) { std::cout << "range_error: " << e.what() << std::endl; } catch(...) { std::cout << "что-то совсем пошло не так" << std::endl; } return 0; }
Conclusion:
0 1 2 range_error: Беда
First, using the ToEnum() function, it is possible to ToEnum() "wrong" values in the exception handling blocks.
Secondly, using the Cast table, simply compare with BoardType::Bad if the exception handling is "annoying".
Thirdly, if there are several dozen "commands", the switch will be worse readable, and perhaps even slower to process.