ZEON256 ZEON256
← BACK

std::numeric_limits<T>::min()

ZEON256
ZEON256 2026-05-22
C++ STL

Usually in C++ I just do INT_MIN/INT_MAX to get the minimum/maximum value of an integer type. But this is not type-safe and certainly not generic. Use std::numeric_limits<T>::min()/std::numeric_limits<T>::max() instead. Example where this shines over INT_MIN/INT_MAX.

C++
template <typename T>
[[nodiscard]]
auto find_max(std::span<const T> data) -> T {
    T hi = std::numeric_limits<T>::lowest();

    for (T d : data) {
        hi = std::max(hi, d);
    }

    return hi;
}

Footgun with floats

In typical C++ fashion, std::numeric_limits<float>::min() does not mean the most negative float.

For floating-point types, min() returns the smallest positive normal value. If you want the lowest representable value, use std::numeric_limits<float>::lowest() instead.