C++ Logo

std-proposals

Advanced search

Re: [std-proposals] all bytes zero == null object

From: Thiago Macieira <thiago_at_[hidden]>
Date: Mon, 22 Jul 2024 11:13:20 -0700
On Monday 22 July 2024 10:48:32 GMT-7 Heckerpowered via Std-Proposals wrote:
> At the beginning of this topic, std::optional needs an additional bytes to
> flag to determine whether a element is empty, so I think it’s not
> necessarily to use a magic value to determine whether a std::optional is
> empty.

We do need it, but it must be determined from an array of bytes, not from an
object whose lifetime cannot exist. The whole point of std::optional is that
the object may not have been not created.

We need a function to initialise the array of bytes to the disengaged state
and another function to look into the array of bytes to say that it is
disengaged.

For example, for QStrings, since the first member is the pointer to the control
block and that can be a null pointer, I wouldn't be able to use bitwise zero
there. But I could use a misaligned value, like uintptr_t(1) or 2 or 3, as
those values can never be correct on any of our supported platforms where
alignof(void *) > 1. For 64-bit platforms, I may even declare that I know all
negative pointers are invalid and use the sign bit.

Or I could use the sign bit of the size field, because no array can be bigger
than PTRDIFF_MAX anyway.

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

Received on 2024-07-22 18:13:25