C++ Logo

std-proposals

Advanced search

Re: [std-proposals] Using -1 as the max value for an unsigned integer

From: Jan Schultke <janschultke_at_[hidden]>
Date: Fri, 18 Jul 2025 12:28:39 +0200
> size_t n = my_string.find("bla bla");
> if ( std::numeric_limits<decltype(n)>::max() == n ) DoSomething();

To be maximally idiomatic, you should be using npos anyway, not rely
on the fact that it's -1, even though that's standard. It doesn't
express intent very well.

If you don't care and want something brief that works in practice, write -1uz.

If you want std::numeric_limits<T>::max() but shorter, write a
max_v<T> variable template.

In any case, I think there's very little motivation for some new type
in the standard library for the sole purpose of "representing a
maximum" generically.

Received on 2025-07-18 10:28:54