3
有沒有辦法知道枚舉值的範圍?我期待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。
不幸的是沒有。有一套複雜的規則決定了有效範圍,但沒有現成的方法來訪問結果。 (另外,規則[據稱是錯誤的](https://github.com/cplusplus/draft/pull/1349)。) –
「範圍」是什麼意思? –
@NeilButterworth http://en.cppreference.com/w/cpp/language/enum並搜索'static_cast'。 –