C++ Logo

std-proposals

Advanced search

Re: [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:22:25 +0100
On Fri, Jul 18, 2025 at 10:15 AM Frederick Virchanza Gotham wrote:
>
> 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 ) { . . . }



I forgot to add that _Max_t would also work properly with types
smaller than int. For example the following program:

    #include <iostream>
    using namespace std;

    int main(void)
    {
        short unsigned m = -1;

        if ( -1 == m )
            cout << "equal\n";
        else
            cout << "not equal\n";
    }

prints out "not equal". But the following program would print out "equal":

    #include <iostream>
    using namespace std;

    int main(void)
    {
        short unsigned m = _Max;

        if ( _Max == m )
            cout << "equal\n";
        else
            cout << "not equal\n";
    }

Received on 2025-07-18 09:22:37