ZEON256 ZEON256
← BACK

noexcept in C++

ZEON256
ZEON256 2026-05-22
C++ STL

noexcept should not be used in these situations

  • passing by value to a function because that may allocate/copy (throw happens on argument construction not function body)
  • in generic code unless you can constrain it properly
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;
}

For generic T, copying T, comparing T, or assigning T might throw. So don’t casually slap noexcept on that version.