如何找到未知类型的最大整数值? 有没有比这更有效的东西:
template<class T>
T test(T i) {
if (((T)-1) > 0)
return -1;
T max_neg = ~(1 << ((sizeof(T)*8)-1));
T all_ones = -1;
T max_pos = all_ones & max_neg;
return max_pos;
}
最佳答案
使用 std::numeric_limits<T>::max()
.自 C++11 起,此函数为 constexpr
并因此在编译时进行评估。
关于c++ - 如何找到未知整数类型的最大值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17521899/