C++ Logo

std-proposals

Advanced search

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

From: Magnus Fromreide <magfr_at_[hidden]>
Date: Mon, 28 Jul 2025 04:22:58 +0200
On Fri, Jul 18, 2025 at 10:15:22AM +0100, Frederick Virchanza Gotham via Std-Proposals wrote:
> Yes I realise that we have:
>
> std::numeric_limits<T>::max()

Yes, std::numeric_limits<T> is annoyingly long and hard to get from a
variable.

When I need to use it I have found a little function like

template <class T>
constexpr std::numeric_limits<typename std::remove_cv<T>::type> bikeshed(T)
{ return { }; }

to be helpful as

bikeshed(n)::max() == n

is simple to write.


> But I always do this:
>
> size_t n = my_string.find("bla bla");
> if ( -1 == n ) DoSomething();
>
> I'm not keen on:
>
> size_t n = my_string.find("bla bla");
> if ( std::numeric_limits<decltype(n)>::max() == n ) DoSomething();
>
> Nor am I keen on:
>
> size_t n = my_string.find("bla bla");
> if ( string::npos == n ) DoSomething();

Here I would write

        if (my_string.npos == n)
                DoSomething();


> And it's annoying when a compiler emits a warning for "-1 == n" to say
> that I'm comparing signed with unsigned.

Received on 2025-07-28 02:23:05