<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Issue 2219: INVOKE-ing a pointer to member with a reference_wrapper as the object expression</title>
<meta property="og:title" content="Issue 2219: INVOKE-ing a pointer to member with a reference_wrapper as the object expression">
<meta property="og:description" content="C++ library issue. Status: C++17">
<meta property="og:url" content="https://cplusplus.github.io/LWG/issue2219.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#C++17">C++17</a> status.</em></p>
<h3 id="2219"><a href="lwg-defects.html#2219">2219</a>. <code><i>INVOKE</i></code>-ing a pointer to member with a <code>reference_wrapper</code> as the object expression</h3>
<p><b>Section:</b> 22.10.4 <a href="https://wg21.link/func.require">[func.require]</a> <b>Status:</b> <a href="lwg-active.html#C++17">C++17</a>
 <b>Submitter:</b> Jonathan Wakely <b>Opened:</b> 2012-11-28 <b>Last modified:</b> 2017-07-30</p>
<p><b>Priority: </b>2
</p>
<p><b>View other</b> <a href="lwg-index-open.html#func.require">active issues</a> in [func.require].</p>
<p><b>View all other</b> <a href="lwg-index.html#func.require">issues</a> in [func.require].</p>
<p><b>View all issues with</b> <a href="lwg-status.html#C++17">C++17</a> status.</p>
<p><b>Discussion:</b></p>

<p>
The standard currently requires this to be invalid:
</p>
<blockquote><pre>
#include &lt;functional&gt;

struct X { int i; } x;
auto f = &amp;X::i;
auto t1 = std::ref(x);
int i = std::mem_fn(f)(t1);
</pre></blockquote>
<p>
The call expression on the last line is equivalent to <code><i>INVOKE</i>(f, std::ref(x))</code> 
which according to 22.10.4 <a href="https://wg21.link/func.require">[func.require]</a>p1 results in the invalid expression <code>(*t1).*f</code> 
because <code>reference_wrapper&lt;X&gt;</code> is neither an object of type <code>X</code> nor a reference 
to an object of type <code>X</code> nor a reference to an object of a type derived from <code>X</code>.
</p>
<p>
The same argument applies to pointers to member functions, and if they don't work with <code>INVOKE</code> 
it becomes harder to do all sorts of things such as:
</p>
<blockquote><p>
<code>call_once(o, &amp;std::thread::join, std::ref(thr))</code>
</p></blockquote>
<p>
or
</p>
<blockquote><p>
<code>async(&amp;std::list&lt;int&gt;::sort, std::ref(list));</code>
</p></blockquote>
<p>
The definition of <code><i>INVOKE</i></code> should be extended to handle reference wrappers.
</p>

<p><i>[2013-03-15 Issues Teleconference]</i></p>

<p>
Moved to Review.
</p>
<p>
The wording seems accurate, but verbose.  If possible, we would like to define the kind of thing being
specified so carefully as one of a number of potential language constructs in a single place.  It is
also possible that this clause <i>is</i> that single place.
</p>

<p><i>[2013-04-18, Bristol]</i></p>


<p>Jonathan comments:</p>

<p>In the proposed resolution in the first bullet <code>(t1.*f)</code> is not valid if <code>t1</code> is a
<code>reference_wrapper</code>, so we probably need a separate bullet to handle the
<code>reference_wrapper</code> case.</p>

<p><i>[2014-02-14, Issaquah, Mike Spertus supplies wording]</i></p>


<p><strong>Previous resolution from Jonathan [SUPERSEDED]:</strong></p>

<blockquote class="note">

<p>This wording is relative to N3485.</p>

<ol>
<li><p>Edit 22.10.4 <a href="https://wg21.link/func.require">[func.require]</a>:</p>
<blockquote><p>
Define <code><i>INVOKE</i>(f, t1, t2, ..., tN)</code> as follows:
</p>
<ul>
<li><p>
<code>(t1.*f)(t2, ..., tN)</code> when <code>f</code> is a pointer to a member function of a class <code>T</code> and 
<code>t1</code> is an object of type <code>T</code> or a reference to an object of type <del><code>T</code> or a reference 
to an object of a type derived from <code>T</code></del> <ins><code>U</code> or an object of type <code>reference_wrapper&lt;U&gt;</code> 
or a reference to an object of type <code>reference_wrapper&lt;U&gt;</code> where <code>U</code> is either the type 
<code>T</code> or a type derived from <code>T</code></ins>;
</p>
</li>

<li><p>
<code>((*t1).*f)(t2, ..., tN)</code> when <code>f</code> is a pointer to a member function of a class <code>T</code> and 
<code>t1</code> is not one of the types described in the previous item;
</p></li>

<li><p> 
<code>t1.*f</code> when <code>N == 1</code> and <code>f</code> is a pointer to member data of a class <code>T</code> and <code>t1</code> 
is an object of type <code>T</code> or a reference to an object of type <del><code>T</code> or a reference to an object of a 
type derived from <code>T</code></del> <ins><code>U</code> or an object of type <code>reference_wrapper&lt;U&gt;</code> 
or a reference to an object of type <code>reference_wrapper&lt;U&gt;</code> where <code>U</code> is either the type 
<code>T</code> or a type derived from <code>T</code></ins>;
</p></li>

<li><p>
<code>(*t1).*f</code> when <code>N == 1</code> and <code>f</code> is a pointer to member data of a class <code>T</code> and <code>t1</code> 
is not one of the types described in the previous item;
</p></li>

<li><p>
<code>f(t1, t2, ..., tN)</code> in all other cases.
</p></li>
</ul>

</blockquote>

</li>
</ol>

</blockquote>
 
<p><i>[2014-10-01, STL adds discussion and provides an improved resolution]</i></p>

<p>
Because neither <code>t1.*f</code> nor <code>(*t1).*f</code> will compile when <code>t1</code> is <code>reference_wrapper&lt;U&gt;</code> 
for any <code>U</code>, we don't need to inspect <code>U</code> carefully. We can bluntly detect all <code>reference_wrapper</code>s 
and use <code>get()</code> for them.
<p/>
We would have to be more careful if we had to deal with pointers to members of <code>reference_wrapper</code> itself.  
Fortunately, we don't. First, it doesn't have user-visible data members. Second, users technically can't take the 
addresses of its member functions (this is a consequence of 16.4.6.5 <a href="https://wg21.link/member.functions">[member.functions]</a>, the Implementer's Best Friend).
<p/>
While we're in the neighborhood, I recommend simplifying and clarifying the wording used to detect base/derived objects.
</p>

<p><strong>Previous resolution from Mike Spertus [SUPERSEDED]:</strong></p>

<blockquote class="note">
<p>This wording is relative to N3936.</p>

<ol>
<li><p>Edit 22.10.4 <a href="https://wg21.link/func.require">[func.require]</a>:</p>
<blockquote><p>
Define <code><i>INVOKE</i>(f, t1, t2, ..., tN)</code> as follows:
</p>
<ul>
<li><p>
<code>(t1.*f)(t2, ..., tN)</code> when <code>f</code> is a pointer to a member function of a class <code>T</code> and 
<code>t1</code> is an object of type <code>T</code> or a reference to an object of type <code>T</code> or a reference 
to an object of a type derived from <code>T</code>;
</p>
</li>

<li><p>
<ins>
<code>(t1.get().*f)(t2, ..., tN)</code> when <code>f</code> is a pointer to a member function
of class <code>T</code>
and <code>t1</code> is an object of type <code>reference_wrapper&lt;U&gt;</code> where <code>U</code>
is either the type <code>T</code> or a type derived from <code>T</code>.
</ins>
</p>
</li>

<li><p>
<code>((*t1).*f)(t2, ..., tN)</code> when <code>f</code> is a pointer to a member function of a class <code>T</code> and 
<code>t1</code> is not one of the types described in the previous item;
</p></li>



<li><p> 
<code>t1.*f</code> when <code>N == 1</code> and <code>f</code> is a pointer to member data of a class <code>T</code> and <code>t1</code> 
is an object of type <code>T</code> or a reference to an object of type <code>T</code> or a reference to an object of a 
type derived from <code>T</code>;
</p></li>

<li><p>
<ins>
<code>t1.get().*f</code> when <code>N == 1</code> and <code>f</code> is a pointer to member data of a
class <code>T</code> and <code>t1</code> is an object of type <code>reference_wrapper&lt;U&gt;</code>
where <code>U</code> is either the type <code>T</code> or a type derived from <code>T</code>.
</ins>
</p></li>

<li><p>
<code>(*t1).*f</code> when <code>N == 1</code> and <code>f</code> is a pointer to member data of a class <code>T</code> and <code>t1</code> 
is not one of the types described in the previous item;
</p></li>

<li><p>
<code>f(t1, t2, ..., tN)</code> in all other cases.
</p></li>
</ul>

</blockquote>

</li>
</ol>
</blockquote>

<p><i>[2015-02, Cologne]</i></p>

<p>
Waiting for implementation experience. 
</p>

<p><i>[2015-05, Lenexa]</i></p>

<p>
STL: latest note from Cologne, waiting for implementation experience<br/>
STL: don't think this is harder than anything else we do<br/>
MC: it does involve <code>mem_fn</code> and invoke<br/>
STL: my simplication was not to attempt fine-grained<br/>
STL: can ignore pmf<br/>
STL: can't invoke pmf to reference wrapper<br/>
STL: wording dated back to TR1 when there was no <code>decltype</code><br/>
MC: should <code>decay_t&lt;decltype(t1)&gt;</code> be pulled out since it is in multiple places<br/>
STL: it could be handled editorially<br/>
STL: we fix function, bind, invoke<br/>
STL: have not implemented this but believe it is fine<br/>
MC: Eric F, you have worked in invoke<br/>
EF: yes, looks ok<br/>
MC: consensus move to ready 
</p>


<p id="res-2219"><b>Proposed resolution:</b></p>
<p>This wording is relative to N3936.</p>

<ol>
<li><p>Change 22.10.4 <a href="https://wg21.link/func.require">[func.require]</a> p1 as depicted:</p>
<blockquote><p>
Define <code><i>INVOKE</i>(f, t1, t2, ..., tN)</code> as follows:
</p>
<ul>
<li><p>
<code>(t1.*f)(t2, ..., tN)</code> when <code>f</code> is a pointer to a member function of a class <code>T</code> and 
<del><code>t1</code> is an object of type <code>T</code> or a reference to an object of type <code>T</code> or a reference 
to an object of a type derived from <code>T</code></del><ins><code>is_base_of&lt;T, decay_t&lt;decltype(t1)&gt;&gt;::value</code> 
is true</ins>;
</p>
</li>

<li><p>
<ins><code>(t1.get().*f)(t2, ..., tN)</code> when <code>f</code> is a pointer to a member function of a class <code>T</code> and 
<code>decay_t&lt;decltype(t1)&gt;</code> is a specialization of <code>reference_wrapper</code>;</ins>
</p></li>

<li><p>
<code>((*t1).*f)(t2, ..., tN)</code> when <code>f</code> is a pointer to a member function of a class <code>T</code> and 
<code>t1</code> <del>is not one of the types described in the previous item</del><ins>does not satisfy the previous two 
items</ins>;
</p></li>

<li><p> 
<code>t1.*f</code> when <code>N == 1</code> and <code>f</code> is a pointer to member data of a class <code>T</code> and <del><code>t1</code> 
is an object of type <code>T</code> or a reference to an object of type <code>T</code> or a reference to an object of a 
type derived from <code>T</code></del><ins><code>is_base_of&lt;T, decay_t&lt;decltype(t1)&gt;&gt;::value</code> 
is true</ins>;
</p></li>

<li><p>
<ins><code>t1.get().*f</code> when <code>N == 1</code> and <code>f</code> is a pointer to member data of a class <code>T</code> and 
<code>decay_t&lt;decltype(t1)&gt;</code> is a specialization of <code>reference_wrapper</code>;</ins>
</p></li>

<li><p>
<code>(*t1).*f</code> when <code>N == 1</code> and <code>f</code> is a pointer to member data of a class <code>T</code> and <code>t1</code> 
<del>is not one of the types described in the previous item</del><ins>does not satisfy the previous two items</ins>;
</p></li>

<li><p>
<code>f(t1, t2, ..., tN)</code> in all other cases.
</p></li>
</ul>

</blockquote>

</li>
</ol>






</body>
</html>
