Skip to content

Commit 3622cdc

Browse files
committed
New issue from Hewill: "Improve optional<T&>::or_else"
1 parent 1882a7d commit 3622cdc

File tree

1 file changed

+58
-0
lines changed

1 file changed

+58
-0
lines changed

xml/issue4367.xml

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
<?xml version='1.0' encoding='utf-8' standalone='no'?>
2+
<!DOCTYPE issue SYSTEM "lwg-issue.dtd">
3+
4+
<issue num="4367" status="New">
5+
<title>Improve <code>optional&lt;T&amp;&gt;::or_else</code></title>
6+
<section>
7+
<sref ref="[optional.ref.monadic]"/>
8+
</section>
9+
<submitter>Hewill Kang</submitter>
10+
<date>07 Sep 2025</date>
11+
<priority>99</priority>
12+
13+
<discussion>
14+
<p>
15+
<code>optional&lt;T&amp;&gt;::or_else</code> currently returns <code>*<i>val</i></code> when it has a value,
16+
which calls the <code>optional(U&amp;&amp;)</code> constructor which in turn calls
17+
<code><i>convert-ref-init-val</i></code> which in turn calls `addressof`.
18+
<p/>
19+
There's no reason to do that. It's much more efficient to call <code>optional&lt;T&amp;&gt;</code>'s
20+
default copy constructor to just copy a pointer.
21+
</p>
22+
</discussion>
23+
24+
<resolution>
25+
<p>
26+
This wording is relative to <paper num="N5014"/>.
27+
</p>
28+
29+
<ol>
30+
31+
<li><p>Modify <sref ref="[optional.ref.monadic]"/> as indicated:</p>
32+
33+
<blockquote>
34+
<pre>
35+
template&lt;class F&gt; constexpr optional or_else(F&amp;&amp; f) const;
36+
</pre>
37+
<blockquote>
38+
<p>
39+
-7- <i>Constraints</i>: `F` models `invocable`.
40+
<p/>
41+
-8- <i>Mandates</i>: <code>is_same_v&lt;remove_cvref_t&lt;invoke_result_t&lt;F&gt;&gt;, optional&gt;</code> is `true`.
42+
<p/>
43+
-9- <i>Effects</i>: Equivalent to:
44+
</p>
45+
<blockquote><pre>
46+
if (has_value()) {
47+
return *<ins>this</ins><del><i>val</i></del>;
48+
} else {
49+
return std::forward&lt;F&gt;(f)();
50+
}
51+
</pre></blockquote></blockquote>
52+
</blockquote>
53+
54+
</li>
55+
56+
</ol></resolution>
57+
58+
</issue>

0 commit comments

Comments
 (0)