【问题标题】:Is it possible to know the range of an non-fixed enumerator?是否有可能知道非固定枚举数的范围?
【发布时间】:2017-09-14 22:43:08
【问题描述】:

有没有办法知道枚举值的范围?我期待一些像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

【问题讨论】:

  • 很遗憾没有。有一组复杂的规则来确定有效范围,但没有现成的方法来访问结果。 (另外,规则是allegedly wrong。)
  • “范围”是什么意思?
  • @NeilButterworth en.cppreference.com/w/cpp/language/enum 并搜索 static_cast
  • @NeilButterworth:见 [dcl.enum]p8。

标签: c++ enums c++14


【解决方案1】:

没有。

我看过的 C++2a/2b 反射提案似乎足够强大,如果它们缺乏直接特征的话。我不知道它们在 2020 年标准化的可能性,也不知道保证 100% 的最新提案的文本。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2020-12-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-04-07
    相关资源
    最近更新 更多