2017-09-14 57 views
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

+2

不幸的是沒有。有一套複雜的規則決定了有效範圍,但沒有現成的方法來訪問結果。 (另外,規則[據稱是錯誤的](https://github.com/cplusplus/draft/pull/1349)。) –

+0

「範圍」是什麼意思? –

+0

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

回答

2

的C++ 2a/2b的反射提案(S)我已經看過顯得比足夠強的方式更計算此,如果它們缺乏直接的性狀。我不知道他們在2020年標準化的可能性,也不知道最新的提案保證100%。