<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Issue 4141: Improve prohibitions on "additional storage"</title>
<meta property="og:title" content="Issue 4141: Improve prohibitions on &quot;additional storage&quot;">
<meta property="og:description" content="C++ library issue. Status: WP">
<meta property="og:url" content="https://cplusplus.github.io/LWG/issue4141.html">
<meta property="og:type" content="website">
<meta property="og:image" content="http://cplusplus.github.io/LWG/images/cpp_logo.png">
<meta property="og:image:alt" content="C++ logo">
<style>
  p {text-align:justify}
  li {text-align:justify}
  pre code.backtick::before { content: "`" }
  pre code.backtick::after { content: "`" }
  blockquote.note
  {
    background-color:#E0E0E0;
    padding-left: 15px;
    padding-right: 15px;
    padding-top: 1px;
    padding-bottom: 1px;
  }
  ins {background-color:#A0FFA0}
  del {background-color:#FFA0A0}
  table.issues-index { border: 1px solid; border-collapse: collapse; }
  table.issues-index th { text-align: center; padding: 4px; border: 1px solid; }
  table.issues-index td { padding: 4px; border: 1px solid; }
  table.issues-index td:nth-child(1) { text-align: right; }
  table.issues-index td:nth-child(2) { text-align: left; }
  table.issues-index td:nth-child(3) { text-align: left; }
  table.issues-index td:nth-child(4) { text-align: left; }
  table.issues-index td:nth-child(5) { text-align: center; }
  table.issues-index td:nth-child(6) { text-align: center; }
  table.issues-index td:nth-child(7) { text-align: left; }
  table.issues-index td:nth-child(5) span.no-pr { color: red; }
  @media (prefers-color-scheme: dark) {
     html {
        color: #ddd;
        background-color: black;
     }
     ins {
        background-color: #225522
     }
     del {
        background-color: #662222
     }
     a {
        color: #6af
     }
     a:visited {
        color: #6af
     }
     blockquote.note
     {
        background-color: rgba(255, 255, 255, .10)
     }
  }
</style>
</head>
<body>
<hr>
<p><em>This page is a snapshot from the LWG issues list, see the <a href="lwg-active.html">Library Active Issues List</a> for more information and the meaning of <a href="lwg-active.html#WP">WP</a> status.</em></p>
<h3 id="4141"><a href="lwg-defects.html#4141">4141</a>. Improve prohibitions on "additional storage"</h3>
<p><b>Section:</b> 22.5.3.1 <a href="https://wg21.link/optional.optional.general">[optional.optional.general]</a>, 22.6.3.1 <a href="https://wg21.link/variant.variant.general">[variant.variant.general]</a>, 22.8.6.1 <a href="https://wg21.link/expected.object.general">[expected.object.general]</a>, 22.8.7.1 <a href="https://wg21.link/expected.void.general">[expected.void.general]</a> <b>Status:</b> <a href="lwg-active.html#WP">WP</a>
 <b>Submitter:</b> Jonathan Wakely <b>Opened:</b> 2024-08-22 <b>Last modified:</b> 2024-11-28</p>
<p><b>Priority: </b>Not Prioritized
</p>
<p><b>View all other</b> <a href="lwg-index.html#optional.optional.general">issues</a> in [optional.optional.general].</p>
<p><b>View all issues with</b> <a href="lwg-status.html#WP">WP</a> status.</p>
<p><b>Discussion:</b></p>
<p>
This issue was split out from issue <a href="lwg-active.html#4015" title="LWG 3973 broke const overloads of std::optional monadic operations (Status: Open)">4015</a><sup><a href="https://cplusplus.github.io/LWG/issue4015" title="Latest snapshot">(i)</a></sup>.
</p>
<p>
<code class='backtick'>optional</code>, <code class='backtick'>variant</code> and <code class='backtick'>expected</code> all use similar wording to require
their contained value to be a subobject, rather than dynamically allocated
and referred to by a pointer, e.g.
<blockquote>
When an instance of <code>optional&lt;T&gt;</code> contains a value,
it means that an object of type <code>T</code>, referred to as the
optional object’s <i>contained value</i>,
is allocated within the storage of the optional object.
Implementations are not permitted to use additional storage,
such as dynamic memory, to allocate its contained value.
</blockquote>
</p>

<p>
During the LWG reviews of <a href="https://wg21.link/P2300" title=" `std::execution`">P2300</a> in St. Louis, concerns were
raised about the form of this wording and whether it's normatively meaningful.
Except for the special case of standard-layout class types, the standard has
very few requirements on where or how storage for subobjects is allocated.
The library should not be trying to dictate more than the language guarantees.
It would be better to refer to wording from 6.8.2 <a href="https://wg21.link/intro.object">[intro.object]</a>
such as <i>subobject</i>, <i>provides storage</i>, or <i>nested within</i>.
Any of these terms would provide the desired properties, without using
different (and possibly inconsistent) terminology.
</p>
<p>
Using an array of bytes to <i>provide storage</i> for the contained value would
make it tricky to meet the constexpr requirements of types like <code class='backtick'>optional</code>.
This means in practice, the most restrictive of these terms, <i>subobject</i>,
is probably accurate and the only plausible implementation strategy.
However, I don't see any reason to outlaw other implementation strategies that
might be possible in future (say, with a constexpr type cast, or non-standard
compiler-specific instrinics).
For this reason, the proposed resolution below uses <i>nested within</i>,
which provides the desired guarantee without imposing additional restrictions
on implementations.
</p>

<p><i>[2024-09-18; Reflector poll]</i></p>

<p>
Set status to Tentatively Ready after seven votes in favour during reflector poll.
</p>

<p><i>[Wrocław 2024-11-23; Status changed: Voting &rarr; WP.]</i></p>



<p id="res-4141"><b>Proposed resolution:</b></p>
<p>
This wording is relative to <a href="https://wg21.link/N4988" title=" Working Draft, Programming Languages — C++">N4988</a>.
</p>

<ol>
<li><p>Modify 22.5.3.1 <a href="https://wg21.link/optional.optional.general">[optional.optional.general]</a> as indicated:</p>

<blockquote class="note">
[<i>Drafting note</i>:
This edit modifies the same paragraph as issue <a href="lwg-active.html#4015" title="LWG 3973 broke const overloads of std::optional monadic operations (Status: Open)">4015</a><sup><a href="https://cplusplus.github.io/LWG/issue4015" title="Latest snapshot">(i)</a></sup>,
but that other issue intentionally doesn't touch the affected sentence here
(except for removing the italics on "contained value").
The intention is that the merge conflict can be resolved in the obvious way:
"An optional object's contained value
is nested within (6.8.2 <a href="https://wg21.link/intro.object">[intro.object]</a>) the optional object."]
</blockquote>

<blockquote>
<p>
-1-
Any instance of <code>optional&lt;T&gt;</code> at any given time either
contains a value or does not contain a value.
When an instance of <code>optional&lt;T&gt;</code> <i>contains a value</i>,
it means that an object of type <code>T</code>,
referred to as the optional object's <i>contained value</i>,
is
<del>allocated within the storage of</del>
<ins>nested within (6.8.2 <a href="https://wg21.link/intro.object">[intro.object]</a>)</ins>
the optional object.
<del>
Implementations are not permitted to use additional storage,
such as dynamic memory, to allocate its contained value.
</del>
When an object of type <code>optional&lt;T&gt;</code>
is contextually converted to <code>bool</code>,
the conversion returns <code class='backtick'>true</code> if the object contains a value;
otherwise the conversion returns <code class='backtick'>false</code>.
</p>
</blockquote>
</li>

<li><p>Modify 22.6.3.1 <a href="https://wg21.link/variant.variant.general">[variant.variant.general]</a> as indicated:</p>

<blockquote>
<p>
-1-
Any instance of <code>variant</code> at any given time either
holds a value of one of its alternative types or holds no value.
When an instance of <code>variant</code> holds a value of alternative type <code class='backtick'>T</code>,
it means that a value of type <code>T</code>,
referred to as the <code class='backtick'>variant</code> object's <i>contained value</i>,
is
<del>allocated within the storage of</del>
<ins>nested within (6.8.2 <a href="https://wg21.link/intro.object">[intro.object]</a>)</ins>
the <code class='backtick'>variant</code> object.
<del>
Implementations are not permitted to use additional storage,
such as dynamic memory, to allocate the contained value.
</del>
</p>
</blockquote>
</li>

<li><p>Modify 22.8.6.1 <a href="https://wg21.link/expected.object.general">[expected.object.general]</a> as indicated:</p>

<blockquote>
<p>
-1-
Any object of type <code>expected&lt;T, E&gt;</code> either contains
a value of type <code class='backtick'>T</code> or a value of type <code class='backtick'>E</code>
<del>within its own storage</del>
<ins>nested within (6.8.2 <a href="https://wg21.link/intro.object">[intro.object]</a>) it</ins>.
<del>
Implementations are not permitted to use additional storage,
such as dynamic memory,
to allocate the object of type <code class='backtick'>T</code> or the object of type <code class='backtick'>E</code>.
</del>
Member <i><code>has_val</code></i> indicates whether the
<code>expected&lt;T, E&gt;</code> object contains an object of type <code class='backtick'>T</code>.
</p>
</blockquote>
</li>

<li><p>Modify 22.8.7.1 <a href="https://wg21.link/expected.void.general">[expected.void.general]</a> as indicated:</p>

<blockquote>
<p>
-1-
Any object of type <code>expected&lt;T, E&gt;</code> either represents
a value of type <code class='backtick'>T</code>, or contains a value of type <code class='backtick'>E</code>
<del>within its own storage</del>
<ins>nested within (6.8.2 <a href="https://wg21.link/intro.object">[intro.object]</a>) it</ins>.
<del>
Implementations are not permitted to use additional storage,
such as dynamic memory, to allocate the object of type <code class='backtick'>E</code>.
</del>
Member <i><code>has_val</code></i> indicates whether the
<code>expected&lt;T, E&gt;</code> represents a value of type <code class='backtick'>T</code>.
</p>
</blockquote>
</li>
</ol>






</body>
</html>
