|
| 1 | +<!DOCTYPE html> |
| 2 | +<html lang="en"> |
| 3 | +<head> |
| 4 | +<meta charset="utf-8"> |
| 5 | +<title>Issue 4406: optional::value_or return statement is inconsistent with Mandates</title> |
| 6 | +<meta property="og:title" content="Issue 4406: optional::value_or return statement is inconsistent with Mandates"> |
| 7 | +<meta property="og:description" content="C++ library issue. Status: New"> |
| 8 | +<meta property="og:url" content="https://cplusplus.github.io/LWG/issue4406.html"> |
| 9 | +<meta property="og:type" content="website"> |
| 10 | +<meta property="og:image" content="http://cplusplus.github.io/LWG/images/cpp_logo.png"> |
| 11 | +<meta property="og:image:alt" content="C++ logo"> |
| 12 | +<style> |
| 13 | + p {text-align:justify} |
| 14 | + li {text-align:justify} |
| 15 | + pre code.backtick::before { content: "`" } |
| 16 | + pre code.backtick::after { content: "`" } |
| 17 | + blockquote.note |
| 18 | + { |
| 19 | + background-color:#E0E0E0; |
| 20 | + padding-left: 15px; |
| 21 | + padding-right: 15px; |
| 22 | + padding-top: 1px; |
| 23 | + padding-bottom: 1px; |
| 24 | + } |
| 25 | + ins {background-color:#A0FFA0} |
| 26 | + del {background-color:#FFA0A0} |
| 27 | + table.issues-index { border: 1px solid; border-collapse: collapse; } |
| 28 | + table.issues-index th { text-align: center; padding: 4px; border: 1px solid; } |
| 29 | + table.issues-index td { padding: 4px; border: 1px solid; } |
| 30 | + table.issues-index td:nth-child(1) { text-align: right; } |
| 31 | + table.issues-index td:nth-child(2) { text-align: left; } |
| 32 | + table.issues-index td:nth-child(3) { text-align: left; } |
| 33 | + table.issues-index td:nth-child(4) { text-align: left; } |
| 34 | + table.issues-index td:nth-child(5) { text-align: center; } |
| 35 | + table.issues-index td:nth-child(6) { text-align: center; } |
| 36 | + table.issues-index td:nth-child(7) { text-align: left; } |
| 37 | + table.issues-index td:nth-child(5) span.no-pr { color: red; } |
| 38 | + @media (prefers-color-scheme: dark) { |
| 39 | + html { |
| 40 | + color: #ddd; |
| 41 | + background-color: black; |
| 42 | + } |
| 43 | + ins { |
| 44 | + background-color: #225522 |
| 45 | + } |
| 46 | + del { |
| 47 | + background-color: #662222 |
| 48 | + } |
| 49 | + a { |
| 50 | + color: #6af |
| 51 | + } |
| 52 | + a:visited { |
| 53 | + color: #6af |
| 54 | + } |
| 55 | + blockquote.note |
| 56 | + { |
| 57 | + background-color: rgba(255, 255, 255, .10) |
| 58 | + } |
| 59 | + } |
| 60 | +</style> |
| 61 | +</head> |
| 62 | +<body> |
| 63 | +<hr> |
| 64 | +<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#New">New</a> status.</em></p> |
| 65 | +<h3 id="4406"><a href="lwg-active.html#4406">4406</a>. <code class='backtick'>optional::value_or</code> return statement is inconsistent with <i>Mandates</i></h3> |
| 66 | +<p><b>Section:</b> 22.5.3.7 <a href="https://wg21.link/optional.observe">[optional.observe]</a>, 22.5.4.6 <a href="https://wg21.link/optional.ref.observe">[optional.ref.observe]</a>, 22.8.6.6 <a href="https://wg21.link/expected.object.obs">[expected.object.obs]</a> <b>Status:</b> <a href="lwg-active.html#New">New</a> |
| 67 | + <b>Submitter:</b> Hewill Kang <b>Opened:</b> 2025-09-06 <b>Last modified:</b> 2025-10-10</p> |
| 68 | +<p><b>Priority: </b>Not Prioritized |
| 69 | +</p> |
| 70 | +<p><b>View other</b> <a href="lwg-index-open.html#optional.observe">active issues</a> in [optional.observe].</p> |
| 71 | +<p><b>View all other</b> <a href="lwg-index.html#optional.observe">issues</a> in [optional.observe].</p> |
| 72 | +<p><b>View all issues with</b> <a href="lwg-status.html#New">New</a> status.</p> |
| 73 | +<p><b>Discussion:</b></p> |
| 74 | +<p> |
| 75 | +<code>optional<T>::value_or(U&&)</code> requires <code>is_convertible_v<U&&, T></code> |
| 76 | +to ensure that <code class='backtick'>T</code> can be convert from <code class='backtick'>U</code> when <code class='backtick'>optional</code> has no value. |
| 77 | +<p/> |
| 78 | +However, the return statement explicitly constructs <code class='backtick'>T</code> by <code class='backtick'>static_cast</code>, which is not checked by |
| 79 | +<code class='backtick'>is_convertible_v</code> since it only checks for implicit conversions. |
| 80 | +<p/> |
| 81 | +This results in rare cases where <i>Mandates</i> may not be violated, but <code class='backtick'>value_or</code> is ill-formed |
| 82 | +(<a href="https://godbolt.org/z/z5jjhY7c4">demo</a>): |
| 83 | +</p> |
| 84 | +<blockquote><pre> |
| 85 | +struct S { |
| 86 | + operator int() const; |
| 87 | + explicit operator int() = delete; |
| 88 | +}; |
| 89 | + |
| 90 | +int main() { |
| 91 | + std::optional<int>{}.value_or(S{}); // <span style="color:#C80000;font-weight:bold">fire</span> |
| 92 | +} |
| 93 | +</pre></blockquote> |
| 94 | +<p> |
| 95 | +It is reasonable to create objects that stick to <i>Mandates</i>. The same goes for <code class='backtick'>expected::value_or</code>. |
| 96 | +<p/> |
| 97 | +<b>Daniel:</b> |
| 98 | +<p/> |
| 99 | +This issue has considerable overlap with LWG <a href="lwg-active.html#4281" title="Inconsistency between value_or() and error_or() in std::expected (Status: New)">4281</a><sup><a href="https://cplusplus.github.io/LWG/issue4281" title="Latest snapshot">(i)</a></sup>. |
| 100 | +</p> |
| 101 | + |
| 102 | + |
| 103 | +<p id="res-4406"><b>Proposed resolution:</b></p> |
| 104 | +<p> |
| 105 | +This wording is relative to <a href="https://wg21.link/N5014">N5014</a>. |
| 106 | +</p> |
| 107 | + |
| 108 | +<ol> |
| 109 | + |
| 110 | +<li><p>Modify 22.5.3.7 <a href="https://wg21.link/optional.observe">[optional.observe]</a> as indicated:</p> |
| 111 | + |
| 112 | +<blockquote> |
| 113 | +<pre> |
| 114 | +template<class U = remove_cv_t<T>> constexpr T value_or(U&& v) const &; |
| 115 | +</pre> |
| 116 | +<blockquote> |
| 117 | +<p> |
| 118 | +-15- <i>Mandates</i>: <code>is_copy_constructible_v<T> && is_convertible_v<U&&, T></code> |
| 119 | +is <code class='backtick'>true</code>. |
| 120 | +<p/> |
| 121 | +-16- <i>Effects</i>: Equivalent to: |
| 122 | +</p> |
| 123 | +<blockquote><pre> |
| 124 | +<del>return has_value() ? **this : static_cast<T>(std::forward<U>(v));</del> |
| 125 | +<ins>if (has_value()) |
| 126 | + return **this; |
| 127 | +return std::forward<U>(v);</ins> |
| 128 | +</pre></blockquote> |
| 129 | +</blockquote> |
| 130 | +<pre> |
| 131 | +template<class U = remove_cv_t<T>> constexpr T value_or(U&& v) &&; |
| 132 | +</pre> |
| 133 | +<blockquote> |
| 134 | +<p> |
| 135 | +-17- <i>Mandates</i>: <code>is_move_constructible_v<T> && is_convertible_v<U&&, T></code> |
| 136 | +is <code class='backtick'>true</code>. |
| 137 | +<p/> |
| 138 | +-18- <i>Effects</i>: Equivalent to: |
| 139 | +</p> |
| 140 | +<blockquote><pre> |
| 141 | +<del>return has_value() ? std::move(**this) : static_cast<T>(std::forward<U>(v));</del> |
| 142 | +<ins>if (has_value()) |
| 143 | + return std::move(**this); |
| 144 | +return std::forward<U>(v);</ins> |
| 145 | +</pre></blockquote> |
| 146 | +</blockquote> |
| 147 | +</blockquote> |
| 148 | +</li> |
| 149 | + |
| 150 | +<li><p>Modify 22.5.4.6 <a href="https://wg21.link/optional.ref.observe">[optional.ref.observe]</a> as indicated:</p> |
| 151 | + |
| 152 | +<blockquote> |
| 153 | +<pre> |
| 154 | +template<class U = remove_cv_t<T>> constexpr remove_cv_t<T> value_or(U&& u) const; |
| 155 | +</pre> |
| 156 | +<blockquote> |
| 157 | +<p> |
| 158 | +-8- Let <code>X</code> be <code>remove_cv_t<T></code>. |
| 159 | +<p/> |
| 160 | +-9- <i>Mandates</i>: <code>is_constructible_v<X, T&> && is_convertible_v<U, X></code> |
| 161 | +is <code class='backtick'>true</code>. |
| 162 | +<p/> |
| 163 | +-10- <i>Effects</i>: Equivalent to: |
| 164 | +</p> |
| 165 | +<blockquote><pre> |
| 166 | +<del>return has_value() ? *<i>val</i> : static_cast<X>(std::forward<U>(u));</del> |
| 167 | +<ins>if (has_value()) |
| 168 | + return *<i>val</i>; |
| 169 | +return std::forward<U>(u); |
| 170 | +</ins> |
| 171 | +</pre></blockquote> |
| 172 | +</blockquote> |
| 173 | +</blockquote> |
| 174 | +</li> |
| 175 | + |
| 176 | +<li><p>Modify 22.8.6.6 <a href="https://wg21.link/expected.object.obs">[expected.object.obs]</a> as indicated:</p> |
| 177 | + |
| 178 | +<blockquote> |
| 179 | +<pre> |
| 180 | +template<class U = remove_cv_t<T>> constexpr T value_or(U&& v) const &; |
| 181 | +</pre> |
| 182 | +<blockquote> |
| 183 | +<p> |
| 184 | +-18- <i>Mandates</i>: <code>is_copy_constructible_v<T></code> is <code class='backtick'>true</code> and |
| 185 | +<code>is_convertible_v<U, T></code> is <code class='backtick'>true</code>. |
| 186 | +<p/> |
| 187 | +<del>-19- <i>Returns</i>: <code>has_value() ? **this : static_cast<T>(std::forward<U>(v))</code>.</del> |
| 188 | +<p/> |
| 189 | +<ins>-?- <i>Effects</i>: Equivalent to:</ins> |
| 190 | +</p> |
| 191 | +<blockquote><pre> |
| 192 | +<ins>if (has_value()) |
| 193 | + return **this; |
| 194 | +return std::forward<U>(v);</ins> |
| 195 | +</pre></blockquote> |
| 196 | +</blockquote> |
| 197 | +<pre> |
| 198 | +template<class U = remove_cv_t<T>> constexpr T value_or(U&& v) &&; |
| 199 | +</pre> |
| 200 | +<blockquote> |
| 201 | +<p> |
| 202 | +-20- <i>Mandates</i>: <code>is_move_constructible_v<T></code> is <code class='backtick'>true</code> and |
| 203 | +<code>is_convertible_v<U, T></code> is <code class='backtick'>true</code>. |
| 204 | +<p/> |
| 205 | +<del>-21- <i>Returns</i>: <code>has_value() ? std::move(**this) : static_cast<T>(std::forward<U>(v))</code>.</del> |
| 206 | +<p/> |
| 207 | +<ins>-?- <i>Effects</i>: Equivalent to:</ins> |
| 208 | +</p> |
| 209 | +<blockquote><pre> |
| 210 | +<ins>if (has_value()) |
| 211 | + return std::move(**this); |
| 212 | +return std::forward<U>(v);</ins> |
| 213 | +</pre></blockquote> |
| 214 | +</blockquote> |
| 215 | +</blockquote> |
| 216 | +</li> |
| 217 | +</ol> |
| 218 | + |
| 219 | + |
| 220 | + |
| 221 | + |
| 222 | + |
| 223 | + |
| 224 | +</body> |
| 225 | +</html> |
0 commit comments