C++ Logo

std-proposals

Advanced search

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

From: Thiago Macieira <thiago_at_[hidden]>
Date: Fri, 18 Jul 2025 12:17:21 -0700
On Friday, 18 July 2025 07:56:59 Pacific Daylight Time Frederick Virchanza
Gotham via Std-Proposals wrote:
> So when I'm dealing with an unsigned integer variable shared between
> multiple threads, I blacklist 0 and its max value as invalid, like this:
>
> if ( 0u==n || -1==n ) . . .
>
> But from now on I'll write:
>
> if ( 0u==n || _Max==n ) . . .

Or

 if (ssize_t(n) <= 0)

and blacklist half of the possible values.

BTW, I advise you use a std::atomic<int> for this, because int can be more
efficiently waited on, unlike size_t.

-- 
Thiago Macieira - thiago (AT) macieira.info - thiago (AT) kde.org
  Principal Engineer - Intel Platform & System Engineering

Received on 2025-07-18 19:17:26