C++ Logo

std-proposals

Advanced search

Re: [std-proposals] zero overhead for std::optional

From: Jeremy Rifkin <jeremy_at_[hidden]>
Date: Sat, 4 Apr 2026 21:46:03 -0500
Hi,
The overhead in std::optional is not a flaw.

There are some types and use cases where it makes sense to use a
sentinel value or where you could store the flag intrusively in the
object. This can't be done for all types and all use cases.

std::optional will not be changed, least for all for ABI, but you
could propose a new type like std::sentinel_optional or
std::intrusive_optional.

> I vibed (used KI claude)

Aside from ISO rules, most people will be put off by AI-generated
content. Use AI as a tool to help your productivity, but anything you
send here should be something you stand by not just AI slop.

Cheers,
Jeremy

On Sat, Apr 4, 2026 at 5:19 AM Peter Neiss via Std-Proposals
<std-proposals_at_[hidden]> wrote:
>
> Hello,
>
> the std::optional as it is today has an overhead flaw with the additional bool in the implementation to check if a value is there. In a lot of major and important cases this is suboptimal. (It is necessary for the general case).
>
> Principal example are pointers which have a NULL that is traditionally used to mark them as "not value".
>
> I vibed (used KI claude) to generate a proposal to fix this in C++ 29.
>
> Please have a look here and the attached proposal.pdf.
>
> https://github.com/PeterNeiss/optional
>
> TL;DR
> sizeof(std::optional<int*>) == 2*sizeof(int*) // 64bit platform: 16Byte
> sizeof(std::optional<int*, nullptr>) == 2*sizeof(int*) // 64bit platform: 8Byte
>
> Yours,
> Peter
> --
> Std-Proposals mailing list
> Std-Proposals_at_[hidden]
> https://lists.isocpp.org/mailman/listinfo.cgi/std-proposals

Received on 2026-04-05 02:46:21