Use std::numeric_limits<T> to query various properties of a numeric type T:
- Use min() and max() static methods to get the smallest and largest finite numbers of a type:
template<typename T, typename I>
T minimum(I const start, I const end)
{
T minval = std::numeric_limits<T>::max();
for (auto i = start; i < end; ++i)
{
if (*i < minval)
minval = *i;
}
return minval;
}
int range[std::numeric_limits<char>::max() + 1] = { 0 };
switch(get_value())
{
case std::numeric_limits<int>::min():
break;
}
- Use other static methods and static constants to retrieve other properties of a numeric type:
auto n = 42;
std::bitset<std::numeric_limits<decltype(n)>::digits>
bits { static_cast<unsigned long long>(n) };
In C++11, there is no limitation to where std::numeric_limits<T> can be used; therefore, preferably use it over C macros in your modern C++ code.