fork download
  1. #include <stdint.h>
  2. #include <iostream>
  3. #include <limits>
  4. #include <type_traits>
  5.  
  6. enum class Color: uint32_t{ red, green, blue, orange=0xFFFFFFFF };
  7.  
  8. int main(){
  9. Color g = Color::green;
  10. static_assert(std::underlying_type_t<Color>(Color::green) <= std::numeric_limits<uint8_t>::max(), "Green overflows!");
  11. auto gg = static_cast<uint8_t>(g);
  12.  
  13. Color f = Color::orange;
  14. static_assert(std::underlying_type_t<Color>(Color::orange) <= std::numeric_limits<uint8_t>::max(), "Orange overflows!");
  15. auto ff = static_cast<uint8_t>(f);
  16.  
  17. printf("Casted value of red: 0x%x\n", gg);
  18. printf("Casted value of orange: 0x%x\n", ff);
  19.  
  20. return 0;
  21. }
Compilation error #stdin compilation error #stdout 0.01s 5432KB
stdin
Standard input is empty
compilation info
prog.cpp: In function ‘int main()’:
prog.cpp:14:64: error: static assertion failed: Orange overflows!
     static_assert(std::underlying_type_t<Color>(Color::orange) <= std::numeric_limits<uint8_t>::max(), "Orange overflows!");
                        ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
stdout
Standard output is empty