C++ Logo

std-proposals

Advanced search

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

From: Frederick Virchanza Gotham <cauldwell.thomas_at_[hidden]>
Date: Fri, 18 Jul 2025 10:15:22 +0100
Yes I realise that we have:

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

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();

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

I wonder could we standardise some shorthand for this? We already have
nullptr_t and nullptr, so maybe we could have _Max_t and _Max?

So it would become:

    size_t n = my_string.find("bla bla");
    if ( _Max == n ) DoSomething();

And of course in our own classes we could do:

    bool MyClass::operator==( _Max_t ) { . . . }
    MyClass &MyClass::operator=( _Max_t ) { . . . }

Received on 2025-07-18 09:15:35