strongly-typed-enum

Why is C++ numeric_limits<enum_type>::max() == 0?

浪尽此生 提交于 2019-11-30 17:47:16
Here's a bit of code that might seem like it would work: #include <cassert> #include <limits> enum test { A = 1 }; int main() { int max = std::numeric_limits<test>::max(); assert(max > 0); } But it fails under both GCC (4.6.2) and clang (2.9) on Linux: max() for enum types is in fact zero! And this remains true even if you use the C++11 enum type specifier to explcitly say what type you want your enum to have. Why is this? And as for the C++11 behavior, is it something explcitly called for? I could find no mention of it in N2347, the paper on Strongly Typed Enums. std::numeric_limits is

Why is C++ numeric_limits<enum_type>::max() == 0?

折月煮酒 提交于 2019-11-30 01:49:59
问题 Here's a bit of code that might seem like it would work: #include <cassert> #include <limits> enum test { A = 1 }; int main() { int max = std::numeric_limits<test>::max(); assert(max > 0); } But it fails under both GCC (4.6.2) and clang (2.9) on Linux: max() for enum types is in fact zero! And this remains true even if you use the C++11 enum type specifier to explcitly say what type you want your enum to have. Why is this? And as for the C++11 behavior, is it something explcitly called for? I

How to automatically convert strongly typed enum into int?

牧云@^-^@ 提交于 2019-11-26 15:02:11
#include <iostream> struct a { enum LOCAL_A { A1, A2 }; }; enum class b { B1, B2 }; int foo(int input) { return input; } int main(void) { std::cout << foo(a::A1) << std::endl; std::cout << foo(static_cast<int>(b::B2)) << std::endl; } The a::LOCAL_A is what the strongly typed enum is trying to achieve, but there is a small difference : normal enums can be converted into integer type, while strongly typed enums can not do it without a cast. So, is there a way to convert a strongly typed enum value into an integer type without a cast? If yes, how? Strongly typed enums aiming to solve multiple

How to automatically convert strongly typed enum into int?

我的梦境 提交于 2019-11-26 03:06:03
问题 #include <iostream> struct a { enum LOCAL_A { A1, A2 }; }; enum class b { B1, B2 }; int foo(int input) { return input; } int main(void) { std::cout << foo(a::A1) << std::endl; std::cout << foo(static_cast<int>(b::B2)) << std::endl; } The a::LOCAL_A is what the strongly typed enum is trying to achieve, but there is a small difference : normal enums can be converted into integer type, while strongly typed enums can not do it without a cast. So, is there a way to convert a strongly typed enum