Make optional < T & >
trivially copyable
- Document number:
- P3836R0
- Date:
2025-09-11 - Audience:
- LEWG
- Project:
- ISO/IEC 14882 Programming Languages — C++, ISO/IEC JTC1/SC22/WG21
- Reply-to:
- Jan Schultke <janschultke@gmail.com>
Nevin Liber <nliber@anl.gov> - GitHub Issue:
- wg21.link/P3836/github
- Source:
- github.com/eisenwave/cpp-proposals/blob/master/src/trivially-copyable-optional-ref.cow
is not guaranteed to be trivially copyable.
We propose to fix this in C++26.
Due to certain design questions that concern LEWG,
this issue is not simply handled as an LWG issue.
Contents
1. Introduction
Making operations (at least construction)
of
trivial is the design intent of [P2988R12]:
3.3 Trivial construction
Construction of
should be trivial, because it is straightforward to implement, and
optional < T & > is trivial. Boost is not.
optional < T >
However, possibly due to a wording oversight,
trivial construction is not actually guaranteed,
nor is trivial copyability in general.
This status quo is inconsistent with the design of
as a whole:
the special members (copy constructor, destructor, etc.) of the primary template
are all specified to be trivial if the relevant operations on
are trivial.
Therefore,
is required to be trivially copyable,
unlike
.
This issue should be fixed in C++26. A US national body comment by Argonne National Laboratory requesting a solution has been submitted.
1.1. Wording problem
Intuitively,
(see [P2988R12]) is a
,
wrapped
.
In fact, the synopsis as specified in ([optional.optional.ref]) is as follows:
If the only non-static data member was
,
would be trivially copyable
according to [class.prop] definition of "class,trivially copyable".
However, the implementation has the freedom to add extra non-static
data members,
so no trivial operation is strictly guaranteed.
2. Motivation
should be guaranteed to be trivially copyable for the following reasons:
- It makes the design consistent with the primary template
.optional < T > -
Trivial copyability is a useful property in this case.
It allows storing
in aoptional < T & >
whose bytes are copied between processes, uploaded from CPU to GPU (e.g. in CUDA), etc.struct -
Trivial copyability also makes
an implicit-lifetime class type, which makes it possible to starts its lifetime in aoptional < T & >
more easily via assignment operator, among other benefits.union
3. Design
There are two plausible options to fix the issue of
:
-
Simply specify
to be trivially copyable.optional < T & > -
Additionally constrain its layout so it actually
becomes a wrapper for a
.T *
We propose the second option
because trivial copyability is much more useful when the layout of a type is somewhat constrained.
This makes it possible to
or to encode
using
with some guarantees regarding its layout.
s" and the types they wrap is different,
but that concern is largely hypothetical.
4. Wording
The wording changes are relative to [N5014].
Immediately following [optional.optional.ref.general] paragraph 1, append a paragraph as follows:
A class type
is a trivially copyable class and standard-layout class ([class.prop]).
A class type
has no non-static data members other than
and no base class
for which
([meta.unary.prop])
is
.