<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Issue 3973: Monadic operations should be ADL-proof</title>
<meta property="og:title" content="Issue 3973: Monadic operations should be ADL-proof">
<meta property="og:description" content="C++ library issue. Status: WP">
<meta property="og:url" content="https://cplusplus.github.io/LWG/issue3973.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="3973"><a href="lwg-defects.html#3973">3973</a>. Monadic operations should be ADL-proof</h3>
<p><b>Section:</b> 22.8.6.7 <a href="https://wg21.link/expected.object.monadic">[expected.object.monadic]</a>, 22.5.3.8 <a href="https://wg21.link/optional.monadic">[optional.monadic]</a> <b>Status:</b> <a href="lwg-active.html#WP">WP</a>
 <b>Submitter:</b> Jiang An <b>Opened:</b> 2023-08-10 <b>Last modified:</b> 2023-11-22</p>
<p><b>Priority: </b>Not Prioritized
</p>
<p><b>View all other</b> <a href="lwg-index.html#expected.object.monadic">issues</a> in [expected.object.monadic].</p>
<p><b>View all issues with</b> <a href="lwg-status.html#WP">WP</a> status.</p>
<p><b>Discussion:</b></p>
<p>
LWG <a href="lwg-defects.html#3938" title="Cannot use std::expected monadic ops with move-only error_type (Status: WP)">3938</a><sup><a href="https://cplusplus.github.io/LWG/issue3938" title="Latest snapshot">(i)</a></sup> switched to use <code>**this</code> to access the value stored in <code>std::expected</code>. 
However, as shown in LWG <a href="lwg-active.html#3969" title="std::ranges::fold_left_first_with_iter should be more ADL-proof (Status: New)">3969</a><sup><a href="https://cplusplus.github.io/LWG/issue3969" title="Latest snapshot">(i)</a></sup>, <code>**this</code> can trigger ADL and find an unwanted overload, 
and thus may caused unintended behavior.
<p/>
Current implementations behave correctly (<a href="https://godbolt.org/z/3b1Tbs1jd">Godbolt link</a>): they 
don't direct use <code>**this</code>, but use the name of the union member instead.
<p/>
Moreover, <a href="https://wg21.link/P2407R5" title=" Freestanding Library: Partial Classes">P2407R5</a> will change the monadic operations of <code>std::optional</code> to use <code>**this</code>, 
which is also problematic.
</p>

<p><i>[2023-09-19; Wording update]</i></p>

<p>
Several people preferred to replace <code>operator*()</code> by the corresponding union members, so this part of the proposed wording 
has been adjusted, which is a rather mechanical replacement.
</p>

<p><i>[2023-10-30; Reflector poll]</i></p>

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

<p><i>[2023-11-11 Approved at November 2023 meeting in Kona. Status changed: Voting &rarr; WP.]</i></p>



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

<ol>

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

<blockquote class="note">
<p>
[<i>Drafting note:</i> Effectively replace all occurrences of <code>**this</code> by <code><i>val</i></code>, except for <code>decltype</code>.]
</p>
</blockquote>

<blockquote>
<pre>
template&lt;class F&gt; constexpr auto and_then(F&amp;&amp; f) &amp;;
template&lt;class F&gt; constexpr auto and_then(F&amp;&amp; f) const &amp;;
</pre>
<blockquote>
<p>
-1- Let <code>U</code> be <code>remove_cvref_t&lt;invoke_result_t&lt;F, decltype(<del>**this</del><ins>(<i>val</i>)</ins>)&gt;&gt;</code>.
<p/>
-2- [&hellip;]
<p/>
-3- [&hellip;]
<p/>
-4- <i>Effects</i>: Equivalent to:
</p>
<blockquote><pre>
if (has_value())
  return invoke(std::forward&lt;F&gt;(f), <del>**this</del><ins><i>val</i></ins>);
else
  return U(unexpect, error());
</pre></blockquote>
</blockquote>
<pre>
template&lt;class F&gt; constexpr auto and_then(F&amp;&amp; f) &amp;&amp;;
template&lt;class F&gt; constexpr auto and_then(F&amp;&amp; f) const &amp;&amp;;
</pre>
<blockquote>
<p>
-5- Let <code>U</code> be <code>remove_cvref_t&lt;invoke_result_t&lt;F, decltype((std::move(<del>**this</del><ins><i>val</i></ins>))&gt;&gt;</code>.
<p/>
-6- [&hellip;]
<p/>
-7- [&hellip;]
<p/>
-8- <i>Effects</i>: Equivalent to:
</p>
<blockquote><pre>
if (has_value())
  return invoke(std::forward&lt;F&gt;(f), std::move(<del>**this</del><ins><i>val</i></ins>));
else
  return U(unexpect, std::move(error()));
</pre></blockquote>
</blockquote>
<pre>
template&lt;class F&gt; constexpr auto or_else(F&amp;&amp; f) &amp;;
template&lt;class F&gt; constexpr auto or_else(F&amp;&amp; f) const &amp;;
</pre>
<blockquote>
<p>
-9- Let <code>G</code> be <code>remove_cvref_t&lt;invoke_result_t&lt;F, decltype(error())&gt;&gt;</code>.
<p/>
-10- <i>Constraints</i>: <code>is_constructible_v&lt;T, decltype(<del>**this</del><ins>(<i>val</i>)</ins>)&gt;</code> is <code>true</code>.
<p/>
-11- [&hellip;]
<p/>
-12- <i>Effects</i>: Equivalent to:
</p>
<blockquote><pre>
if (has_value())
  return G(in_place, <del>**this</del><ins><i>val</i></ins>);
else
  return invoke(std::forward&lt;F&gt;(f), error());
</pre></blockquote>
</blockquote>
<pre>
template&lt;class F&gt; constexpr auto or_else(F&amp;&amp; f) &amp;&amp;;
template&lt;class F&gt; constexpr auto or_else(F&amp;&amp; f) const &amp;&amp;;
</pre>
<blockquote>
<p>
-13- Let <code>G</code> be <code>remove_cvref_t&lt;invoke_result_t&lt;F, decltype(std::move(error()))&gt;&gt;</code>.
<p/>
-14- <i>Constraints</i>: <code>is_constructible_v&lt;T, decltype(std::move(<del>**this</del><ins><i>val</i></ins>))&gt;</code> is <code>true</code>.
<p/>
-15- [&hellip;]
<p/>
-16- <i>Effects</i>: Equivalent to:
</p>
<blockquote><pre>
if (has_value())
  return G(in_place, std::move(<del>**this</del><ins><i>val</i></ins>));
else
  return invoke(std::forward&lt;F&gt;(f), std::move(error()));
</pre></blockquote>
</blockquote>
<pre>
template&lt;class F&gt; constexpr auto transform(F&amp;&amp; f) &amp;;
template&lt;class F&gt; constexpr auto transform(F&amp;&amp; f) const &amp;;
</pre>
<blockquote>
<p>
-17- Let <code>U</code> be <code>remove_cvref_t&lt;invoke_result_t&lt;F, decltype(<del>**this</del><ins>(<i>val</i>)</ins>)&gt;&gt;</code>.
<p/>
-18- [&hellip;]
<p/>
-19- <i>Mandates</i>: <code>U</code> is a valid value type for <code>expected</code>. If <code>is_void_v&lt;U&gt;</code> is <code>false</code>, 
the declaration
</p>
<blockquote><pre>
U u(invoke(std::forward&lt;F&gt;(f), <del>**this</del><ins><i>val</i></ins>));
</pre></blockquote>
<p>
is well-formed.
<p/>
-20- <i>Effects</i>:
</p>
<ol style="list-style-type: none">
<li><p>(20.1) &mdash; [&hellip;]</p></li>
<li><p>(20.2) &mdash; Otherwise, if <code>is_void_v&lt;U&gt;</code> is <code>false</code>, returns an <code>expected&lt;U, E&gt;</code> 
object whose <code><i>has_val</i></code> member is <code>true</code> and <code><i>val</i></code> member is direct-non-list-initialized 
with <code>invoke(std::forward&lt;F&gt;(f), <del>**this</del><ins><i>val</i></ins>)</code>.</p></li>
<li><p>(20.3) &mdash; Otherwise, evaluates <code>invoke(std::forward&lt;F&gt;(f), <del>**this</del><ins><i>val</i></ins>)</code> 
and then returns <code>expected&lt;U, E&gt;()</code>.</p></li>
</ol>
</blockquote>
<pre>
template&lt;class F&gt; constexpr auto transform(F&amp;&amp; f) &amp;&amp;;
template&lt;class F&gt; constexpr auto transform(F&amp;&amp; f) const &amp;&amp;;
</pre>
<blockquote>
<p>
-21- Let <code>U</code> be <code>remove_cvref_t&lt;invoke_result_t&lt;F, decltype(std::move(<del>**this</del><ins><i>val</i></ins>))&gt;&gt;</code>.
<p/>
-22- [&hellip;]
<p/>
-23- <i>Mandates</i>: <code>U</code> is a valid value type for <code>expected</code>. If <code>is_void_v&lt;U&gt;</code> is <code>false</code>, 
the declaration
</p>
<blockquote><pre>
U u(invoke(std::forward&lt;F&gt;(f), std::move(<del>**this</del><ins><i>val</i></ins>)));
</pre></blockquote>
<p>
is well-formed.
<p/>
-24- <i>Effects</i>:
</p>
<ol style="list-style-type: none">
<li><p>(24.1) &mdash; [&hellip;]</p></li>
<li><p>(24.2) &mdash; Otherwise, if <code>is_void_v&lt;U&gt;</code> is <code>false</code>, returns an <code>expected&lt;U, E&gt;</code> 
object whose <code><i>has_val</i></code> member is <code>true</code> and <code><i>val</i></code> member is direct-non-list-initialized 
with <code>invoke(std::forward&lt;F&gt;(f), std::move(<del>**this</del><ins><i>val</i></ins>))</code>.</p></li>
<li><p>(24.3) &mdash; Otherwise, evaluates <code>invoke(std::forward&lt;F&gt;(f), std::move(<del>**this</del><ins><i>val</i></ins>))</code> 
and then returns <code>expected&lt;U, E&gt;()</code>.</p></li>
</ol>
</blockquote>
<pre>
template&lt;class F&gt; constexpr auto transform_error(F&amp;&amp; f) &amp;;
template&lt;class F&gt; constexpr auto transform_error(F&amp;&amp; f) const &amp;;
</pre>
<blockquote>
<p>
-25- Let <code>G</code> be <code>remove_cvref_t&lt;invoke_result_t&lt;F, decltype(error())&gt;&gt;</code>.
<p/>
-26- <i>Constraints</i>: <code>is_constructible_v&lt;T, decltype(<del>**this</del><ins>(<i>val</i>)</ins>)&gt;</code> is <code>true</code>.
<p/>
-27- <i>Mandates</i>: [&hellip;]
<p/>
-28- <i>Returns</i>: If <code>has_value()</code> is <code>true</code>, <code>expected&lt;T, G&gt;(in_place, <del>**this</del><ins><i>val</i></ins>)</code>; 
otherwise, an <code>expected&lt;T, G&gt;</code> object whose <code><i>has_val</i></code> member is <code>false</code> and 
<code><i>unex</i></code> member is direct-non-list-initialized with <code>invoke(std::forward&lt;F&gt;(f), error())</code>.
</p>
</blockquote>
<pre>
template&lt;class F&gt; constexpr auto transform_error(F&amp;&amp; f) &amp;&amp;;
template&lt;class F&gt; constexpr auto transform_error(F&amp;&amp; f) const &amp;&amp;;
</pre>
<blockquote>
<p>
-29- Let <code>G</code> be <code>remove_cvref_t&lt;invoke_result_t&lt;F, decltype(std::move(error()))&gt;&gt;</code>.
<p/>
-30- <i>Constraints</i>: <code>is_constructible_v&lt;T, decltype(std::move(<del>**this</del><ins><i>val</i></ins>))&gt;</code> is <code>true</code>.
<p/>
-31- <i>Mandates</i>: [&hellip;]
<p/>
-32- <i>Returns</i>: If <code>has_value()</code> is <code>true</code>, <code>expected&lt;T, G&gt;(in_place, std::move(<del>**this</del><ins><i>val</i></ins>))</code>; 
otherwise, an <code>expected&lt;T, G&gt;</code> object whose <code><i>has_val</i></code> member is <code>false</code> and 
<code><i>unex</i></code> member is direct-non-list-initialized with <code>invoke(std::forward&lt;F&gt;(f), std::move(error()))</code>.
</p>
</blockquote>
</blockquote>
</li>

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


<blockquote class="note">
<p>
[<i>Drafting note:</i> Effectively replace all occurrences of <code>value()</code> by <code>*val</code>.]
</p>
</blockquote>

<blockquote>
<pre>
template&lt;class F&gt; constexpr auto and_then(F&amp;&amp; f) &amp;;
template&lt;class F&gt; constexpr auto and_then(F&amp;&amp; f) const &amp;;
</pre>
<blockquote>
<p>
-1- Let <code>U</code> be <code>invoke_result_t&lt;F, decltype(<del>value()</del><ins>*val</ins>)&gt;</code>.
<p/>
-2- [&hellip;]
<p/>
-3- <i>Effects</i>: Equivalent to:
</p>
<blockquote><pre>
if (*this) {
  return invoke(std::forward&lt;F&gt;(f), <del>value()</del><ins>*val</ins>);
} else {
  return remove_cvref_t&lt;U&gt;();
}
</pre></blockquote>
</blockquote>
<pre>
template&lt;class F&gt; constexpr auto and_then(F&amp;&amp; f) &amp;&amp;;
template&lt;class F&gt; constexpr auto and_then(F&amp;&amp; f) const &amp;&amp;;
</pre>
<blockquote>
<p>
-4- Let <code>U</code> be <code>invoke_result_t&lt;F, decltype(std::move(<del>value()</del><ins>*val</ins>))&gt;</code>.
<p/>
-5- [&hellip;]
<p/>
-6- <i>Effects</i>: Equivalent to:
</p>
<blockquote><pre>
if (*this) {
  return invoke(std::forward&lt;F&gt;(f), std::move(<del>value()</del><ins>*val</ins>));
} else {
  return remove_cvref_t&lt;U&gt;();
}
</pre></blockquote>
</blockquote>
<pre>
template&lt;class F&gt; constexpr auto transform(F&amp;&amp; f) &amp;;
template&lt;class F&gt; constexpr auto transform(F&amp;&amp; f) const &amp;;
</pre>
<blockquote>
<p>
-7- Let <code>U</code> be <code>remove_cv_t&lt;invoke_result_t&lt;F, decltype(<del>value()</del><ins>*val</ins>)&gt;&gt;</code>.
<p/>
-8- <i>Mandates</i>: <code>U</code> is a non-array object type other than <code>in_place_t</code> or <code>nullopt_t</code>. The declaration
</p>
<blockquote><pre>
U u(invoke(std::forward&lt;F&gt;(f), <del>value()</del><ins>*val</ins>));
</pre></blockquote>
<p>
is well-formed for some invented variable <code>u</code>.
<p/>
[&hellip;]
<p/>
-9- <i>Returns</i>: If <code>*this</code> contains a value, an <code>optional&lt;U&gt;</code> object whose contained value is 
direct-non-list-initialized with <code>invoke(std::forward&lt;F&gt;(f), <del>value()</del><ins>*val</ins>)</code>; otherwise, 
<code>optional&lt;U&gt;()</code>.
</p>
</blockquote>
<pre>
template&lt;class F&gt; constexpr auto transform(F&amp;&amp; f) &amp;&amp;;
template&lt;class F&gt; constexpr auto transform(F&amp;&amp; f) const &amp;&amp;;
</pre>
<blockquote>
<p>
-10- Let <code>U</code> be <code>remove_cv_t&lt;invoke_result_t&lt;F, decltype(std::move(<del>value()</del><ins>*val</ins>))&gt;&gt;</code>.
<p/>
-11- <i>Mandates</i>: <code>U</code> is a non-array object type other than <code>in_place_t</code> or <code>nullopt_t</code>. The declaration
</p>
<blockquote><pre>
U u(invoke(std::forward&lt;F&gt;(f), std::move(<del>value()</del><ins>*val</ins>)));
</pre></blockquote>
<p>
is well-formed for some invented variable <code>u</code>.
<p/>
[&hellip;]
<p/>
-12- <i>Returns</i>: If <code>*this</code> contains a value, an <code>optional&lt;U&gt;</code> object whose contained value is 
direct-non-list-initialized with <code>invoke(std::forward&lt;F&gt;(f), std::move(<del>value()</del><ins>*val</ins>))</code>; otherwise, 
<code>optional&lt;U&gt;()</code>.
</p>
</blockquote>
</blockquote>
</li>
</ol>





</body>
</html>
