是否有可能知道非固定枚举的范围?

问题描述:

有没有办法知道枚举值的范围?我期待std::numeric_limits这样的type_traits模板,但我找不到任何人。是否有可能知道非固定枚举的范围?

例如:

enum enm { A = -5, B = 3 }; 

std::cout << std::range_of_enum<enm>::min() << ', ' 
      << std::range_of_enum<enm>::max() << std::endl; 
// Prints -8, 7 with two's complement representation, and -7, 7 
// with one's complement representation. 

注:对于那些谁不知道什么是“枚举范围”(非格式条款虽然),去this question

+2

不幸的是没有。有一套复杂的规则决定了有效范围,但没有现成的方法来访问结果。 (另外,规则[据称是错误的](https://github.com/cplusplus/draft/pull/1349)。) –

+0

“范围”是什么意思? –

+0

@NeilButterworth http://en.cppreference.com/w/cpp/language/enum并搜索'static_cast'。 –

的C++ 2a/2b的反射提案(S)我已经看过显得比足够强的方式更计算此,如果它们缺乏直接的性状。我不知道他们在2020年标准化的可能性,也不知道最新的提案保证100%。