<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Issue 2895: Passing function types to result_of and is_callable</title>
<meta property="og:title" content="Issue 2895: Passing function types to result_of and is_callable">
<meta property="og:description" content="C++ library issue. Status: Resolved">
<meta property="og:url" content="https://cplusplus.github.io/LWG/issue2895.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#Resolved">Resolved</a> status.</em></p>
<h3 id="2895"><a href="lwg-defects.html#2895">2895</a>. Passing function types to <code>result_of</code> and <code>is_callable</code></h3>
<p><b>Section:</b> 21.3.8 <a href="https://wg21.link/meta.rel">[meta.rel]</a> <b>Status:</b> <a href="lwg-active.html#Resolved">Resolved</a>
 <b>Submitter:</b> Great Britain <b>Opened:</b> 2017-02-03 <b>Last modified:</b> 2020-09-06</p>
<p><b>Priority: </b>Not Prioritized
</p>
<p><b>View other</b> <a href="lwg-index-open.html#meta.rel">active issues</a> in [meta.rel].</p>
<p><b>View all other</b> <a href="lwg-index.html#meta.rel">issues</a> in [meta.rel].</p>
<p><b>View all issues with</b> <a href="lwg-status.html#Resolved">Resolved</a> status.</p>
<p><b>Discussion:</b></p>
<b>Addresses GB 55</b>

<p>It is becoming more and more apparent that using a function type as the template argument to <code>result_of</code> causes 
annoying problems. That was done because C++03 didn't have variadic templates, so it allowed an arbitrary number of types 
to be smuggled into the template via a single parameter, but it's a hack and unnecessary in C++ today. 
<code>result_of&lt;F(Args...)&gt;</code> has absolutely nothing to do with a function type that returns <code>F</code>, and the 
syntactic trickery using a function type has unfortunate consequences such as top-level <i>cv</i>-qualifiers and arrays 
decaying (because those are the rules for function types).</p>
<p>It might be too late to change <code>result_of</code>, but we should not repeat the same mistake for <code>std::is_callable</code>.</p>

<p>Proposed change: Possibly get rid of the <code>is_callable&lt;Fn(ArgTypes?...), R&gt;</code> specialization. Change the 
primary template <code>is_callable&lt;class, class R = void&gt; to is_callable&lt;class Fn, class.. ArgTypes?&gt;</code> and 
define a separate template such as <code>is_callable_r&lt;class R, class Fn, class... ArgTypes?&gt;</code> for the version 
that checks the return type. The resulting inconsistency might need to be resolved/improved upon.
</p>

<p><i>[2017-02, pre-Kona]</i></p>

<p>See also LWG <a href="lwg-defects.html#2927" title="Encoding a functor and argument types as a function signature for is_callable and result_of is fragile (Status: Resolved)">2927</a><sup><a href="https://cplusplus.github.io/LWG/issue2927" title="Latest snapshot">(i)</a></sup>.</p>

<p><i>[2017-02-22, Daniel comments and provides concrete wording]</i></p>

<p>
The approach chosen to resolve this issue is a merger with LWG <a href="lwg-defects.html#2928" title="is_callable is not a good name (Status: Resolved)">2928</a><sup><a href="https://cplusplus.github.io/LWG/issue2928" title="Latest snapshot">(i)</a></sup>, that is the <code>callable</code>
traits are also renamed to <code>invocable</code>.
</p>

<p><strong>Previous resolution [SUPERSEDED]:</strong></p>
<blockquote class="note">
<p>
This wording is relative to <a href="https://wg21.link/n4640">N4640</a>.
</p>

<ol>
<li><p>Modify 21.3.3 <a href="https://wg21.link/meta.type.synop">[meta.type.synop]</a>, header <code>&lt;type_traits&gt;</code> synopsis, as indicated:</p>

<blockquote>
<pre>
[&hellip;]
<i>// 20.15.6, type relations</i>
[&hellip;]

<del>template &lt;class, class R = void&gt; struct is_callable; // not defined
template &lt;class Fn, class... ArgTypes, class R&gt;
struct is_callable&lt;Fn(ArgTypes...), R&gt;;</del>
<ins>template &lt;class Fn, class... ArgTypes&gt; struct is_invocable;
template &lt;class R, class Fn, class... ArgTypes&gt; struct is_invocable_r;</ins>

<del>template &lt;class, class R = void&gt; struct is_nothrow_callable; // not defined
template &lt;class Fn, class... ArgTypes, class R&gt;
struct is_nothrow_callable&lt;Fn(ArgTypes...), R&gt;;</del>
<ins>template &lt;class Fn, class... ArgTypes&gt; struct is_nothrow_invocable;
template &lt;class R, class Fn, class... ArgTypes&gt; struct is_nothrow_invocable_r;</ins>

[&hellip;]

<i>// 20.15.6, type relations</i>
[&hellip;]
<del>template &lt;class T, class R = void&gt; constexpr bool is_callable_v
= is_callable&lt;T, R&gt;::value;
template &lt;class T, class R = void&gt; constexpr bool is_nothrow_callable_v
= is_nothrow_callable&lt;T, R&gt;::value;</del>
<ins>template &lt;class Fn, class... ArgTypes&gt; constexpr bool is_invocable_v
= is_invocable&lt;Fn, ArgTypes...&gt;::value;
template &lt;class R, class Fn, class... ArgTypes&gt; constexpr bool is_invocable_r_v
= is_invocable_r&lt;R, Fn, ArgTypes...&gt;::value;
template &lt;class Fn, class... ArgTypes&gt; constexpr bool is_nothrow_invocable_v
= is_nothrow_invocable&lt;Fn, ArgTypes...&gt;::value;
template &lt;class R, class Fn, class... ArgTypes&gt; constexpr bool is_nothrow_invocable_r_v
= is_nothrow_invocable_r&lt;R, Fn, ArgTypes...&gt;::value;</ins>
[&hellip;]
</pre>
</blockquote>

</li>

<li><p>Modify 21.3.8 <a href="https://wg21.link/meta.rel">[meta.rel]</a>, Table 44 &mdash; "Type relationship predicates", as indicated:</p>

<blockquote>
<table border="1">
<caption>Table 44 &mdash; Type relationship predicates</caption>

<tr>
<td colspan="3" align="center">
<code>[&hellip;]</code>
</td>
</tr>

<tr>
<td>
<code>template &lt;class Fn, class...<br/> 
ArgTypes<del>, class R</del>&gt;<br/> 
struct is_<ins>invocable</ins><del>callable&lt;<br/> 
Fn(ArgTypes...), R&gt;</del>;</code>
</td>
<td>
The expression<br/>
<code><i>INVOKE</i>(declval&lt;Fn&gt;(),<br/>
declval&lt;ArgTypes&gt;()...<del>,<br/>
R</del>)</code> is well formed when treated<br/>
as an unevaluated operand
</td>
<td>
<code>Fn</code><del>, <code>R</code>,</del> and all types in the<br/>
parameter pack <code>ArgTypes</code> shall<br/>
be complete types, <i>cv</i> <code>void</code>, or<br/>
arrays of unknown bound.
</td>
</tr>

<tr>
<td>
<ins><code>template &lt;class R, class Fn, class...<br/> 
ArgTypes&gt;<br/> 
struct is_invocable_r;</code></ins>
</td>
<td>
<ins>The expression<br/>
<code><i>INVOKE</i>(declval&lt;Fn&gt;(),<br/>
declval&lt;ArgTypes&gt;()...,<br/>
R)</code> is well formed when treated<br/>
as an unevaluated operand</ins>
</td>
<td>
<ins><code>Fn</code>, <code>R</code>, and all types in the<br/>
parameter pack <code>ArgTypes</code> shall<br/>
be complete types, <i>cv</i> <code>void</code>, or<br/>
arrays of unknown bound.</ins>
</td>
</tr>

<tr>
<td>
<code>template &lt;class Fn, class...<br/> 
ArgTypes<del>, class R</del>&gt;<br/> 
struct is_nothrow_<ins>invocable</ins><del>callable&lt;<br/> 
Fn(ArgTypes...), R&gt;</del>;</code>
</td>
<td>
<code>is_<ins>invocable</ins><del>callable</del>_v&lt;<br/>
<ins>Fn, ArgTypes...</ins><del>Fn(ArgTypes...), R</del>&gt;</code> is<br/>
<code>true</code> and the expression<br/>
<code><i>INVOKE</i>(declval&lt;Fn&gt;(),<br/>
declval&lt;ArgTypes&gt;()...<del>,<br/>
R</del>)</code> is known not to throw any<br/>
exceptions
</td>
<td>
<code>Fn</code><del>, <code>R</code>,</del> and all types in the<br/>
parameter pack <code>ArgTypes</code> shall<br/>
be complete types, <i>cv</i> <code>void</code>, or<br/>
arrays of unknown bound.
</td>
</tr>

<tr>
<td>
<ins><code>template &lt;class R, class Fn, class...<br/> 
ArgTypes, class R&gt;<br/> 
struct is_nothrow_invocable_r;</code></ins>
</td>
<td>
<ins><code>is_invocable_r_v&lt;<br/>
R, Fn, ArgTypes...&gt;</code> is<br/>
<code>true</code> and the expression<br/>
<code><i>INVOKE</i>(declval&lt;Fn&gt;(),<br/>
declval&lt;ArgTypes&gt;()...,<br/>
R)</code> is known not to throw any<br/>
exceptions</ins>
</td>
<td>
<ins><code>Fn</code>, <code>R</code>, and all types in the<br/>
parameter pack <code>ArgTypes</code> shall<br/>
be complete types, <i>cv</i> <code>void</code>, or<br/>
arrays of unknown bound.</ins>
</td>
</tr>

</table>
</blockquote>
</li>

</ol>
</blockquote>

<p><i>[2017-02-24, Daniel comments]</i></p>

<p>
I suggest to apply the paper d0604r0 instead, available on the Kona LWG wiki.
</p>

<p><i>[2017-03-12, post-Kona]</i></p>

<p>Resolved by p0604r0.</p>


<p id="res-2895"><b>Proposed resolution:</b></p>





</body>
</html>
