fork download
  1. #include <iostream>
  2.  
  3. enum class BoardType : uint8_t {
  4. Classic = 0,
  5. Static = 1,
  6. Smart = 2,
  7. Bad = 255
  8. };
  9.  
  10. BoardType Cast[UINT8_MAX]; // <- сюда просто забить константы -++
  11. //
  12. inline BoardType ToEnum(const uint8_t i) { //
  13. BoardType Ret = Cast[i]; //
  14. if (Ret == BoardType::Bad) throw std::range_error("Беда"); //
  15. return Ret; //
  16. } //
  17. //
  18. int main () { //
  19. ////// вместо инициализации ниже - ///////////////////////////////
  20. for(auto i=0; i<UINT8_MAX; i++) Cast[i] = BoardType::Bad;
  21. Cast[0] = BoardType::Classic;
  22. Cast[1] = BoardType::Static;
  23. Cast[2] = BoardType::Smart;
  24. //////////////////////////////////////////////////////////////////
  25. try {
  26. std::cout << static_cast<int>(ToEnum(0)) << std::endl;
  27. std::cout << static_cast<int>(ToEnum(1)) << std::endl;
  28. std::cout << static_cast<int>(ToEnum(2)) << std::endl;
  29. std::cout << static_cast<int>(ToEnum(3)) << std::endl;
  30. } catch(std::range_error &e) {
  31. std::cout << "range_error: " << e.what() << std::endl;
  32. } catch(...) {
  33. std::cout << "что-то совсем пошло не так" << std::endl;
  34. }
  35. return 0;
  36. }
  37.  
Success #stdin #stdout 0s 3468KB
stdin
Standard input is empty
stdout
0
1
2
range_error: Беда