下面是一些看起来可行的代码:
#include <cassert>
#include <limits>
enum test { A = 1 };
int main()
{
int max = std::numeric_limits<test>::max();
assert(max > 0);
}
但它在 Linux 上的 GCC (4.6.2) 和 clang (2.9) 下都失败了:枚举类型的 max() 实际上为零!即使您使用 C++11 枚举类型说明符明确说明您希望枚举具有什么类型,这仍然是正确的。
为什么是这样?至于 C++11 的行为,是否有明确要求?我在关于强类型枚举的论文 N2347 中没有提到它。