C++ Logo

std-proposals

Advanced search

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

From: Peter Neiss <peter.stefan.neiss_at_[hidden]>
Date: Sat, 4 Apr 2026 12:18:59 +0200
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

Received on 2026-04-04 10:19:12