<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Issue 1270: result_of should be moved to &lt;type_traits&gt;</title>
<meta property="og:title" content="Issue 1270: result_of should be moved to &lt;type_traits&gt;">
<meta property="og:description" content="C++ library issue. Status: C++11">
<meta property="og:url" content="https://cplusplus.github.io/LWG/issue1270.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++11">C++11</a> status.</em></p>
<h3 id="1270"><a href="lwg-defects.html#1270">1270</a>. <code>result_of</code> should be moved to <code>&lt;type_traits&gt;</code></h3>
<p><b>Section:</b> 99 [func.ret] <b>Status:</b> <a href="lwg-active.html#C++11">C++11</a>
 <b>Submitter:</b> Alisdair Meredith <b>Opened:</b> 2009-11-19 <b>Last modified:</b> 2016-01-28</p>
<p><b>Priority: </b>Not Prioritized
</p>
<p><b>View all other</b> <a href="lwg-index.html#func.ret">issues</a> in [func.ret].</p>
<p><b>View all issues with</b> <a href="lwg-status.html#C++11">C++11</a> status.</p>
<p><b>Discussion:</b></p>
<p><b>Addresses UK 198</b></p>

<p>
NB Comment: UK-198 makes this request among others.  It refers to a more
detailed issue that BSI did not manage to submit by the CD1 ballot deadline
though.
</p>

<p>
<code>result_of</code> is essentially a metafunction to return the type of an
expression, and belongs with the other library metafunctions in
<code>&lt;type_traits&gt;</code> rather than lurking in <code>&lt;functional&gt;</code>.
 The current definition in <code>&lt;functional&gt;</code> made sense when
<code>result_of</code> was nothing more than a protocol to enable several components
in <code>&lt;functional&gt;</code> to provide their own result types, but it has
become a more general tool.  For instance, <code>result_of</code> is now used in the
threading and futures components.
</p>

<p>
Now that <code>&lt;type_traits&gt;</code> is a required header for free-standing
implementations it will be much more noticeable (in such environments) that a
particularly useful trait is missing, unless that implementation also chooses to
offer <code>&lt;functional&gt;</code> as an extension.
</p>

<p>
The simplest proposal is to simply move the wording (editorial direction below)
although a more consistent form for type_traits would reformat this as a table.
</p>

<p>
Following the acceptance of <a href="lwg-defects.html#1255" title="declval should be added to the library (Status: C++11)">1255</a><sup><a href="https://cplusplus.github.io/LWG/issue1255" title="Latest snapshot">(i)</a></sup>, <code>result_of</code> now
depends on the <code>declval</code> function template, tentatively provided
in <code>&lt;utility&gt;</code> which is <em>not</em> (yet) required of a
free-standing implementation.
</p>

<p>
This dependency is less of an issue when <code>result_of</code> continues to
live in <code>&lt;functional&gt;</code>.
</p>

<p>
Personally, I would prefer to clean up the dependencies so both
<code>result_of</code> and <code>declval</code> are available in a free-standing
implementation, but that would require slightly more work than suggested
here.  A minimal tweak would be to require <code>&lt;utility&gt;</code> in a
free-standing implementation, although there are a couple of subtle
issues with <code>make_pair</code>, which uses <code>reference_wrapper</code> in
its protocol and that is much harder to separate cleanly from
<code>&lt;functional&gt;</code>.
</p>

<p>
An alternative would be to enact the other half of
<a href="http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2009/n2979.html">N2979</a>
and create a new minimal header for the new C++0x library facilities to
be added to the freestanding requirements (plus <code>swap</code>.)
</p>

<p>
I have a mild preference for the latter, although there are clearly
reasons to consider better library support for free-standing in general,
and adding the whole of <code>&lt;utility&gt;</code> could be considered a step in that
direction.  See NB comment
<a href="http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2009/n3009.html#JP23">JP-23</a>
for other suggestions (<code>array</code>, <code>ratio</code>)
</p>

<p><i>[
2010-01-27 Beman updated wording.
]</i></p>


<blockquote>
<p>
The original wording is preserved here:
</p>
<blockquote>

<p>
Move 99 [func.ret] to a heading below 21 <a href="https://wg21.link/meta">[meta]</a>.  Note
that in principle we should not change the tag, although this is a new tag for
0x.  If it has been stable since TR1 it is clearly immutable though.
</p>

<p>
This wording should obviously adopt any other changes currently in (Tentatively)
Ready status that touch this wording, such as <a href="lwg-defects.html#1255" title="declval should be added to the library (Status: C++11)">1255</a><sup><a href="https://cplusplus.github.io/LWG/issue1255" title="Latest snapshot">(i)</a></sup>.
</p>

</blockquote>
</blockquote>

<p><i>[
2010-02-09 Moved to Tentatively Ready after 5 positive votes on c++std-lib.
]</i></p>




<p id="res-1270"><b>Proposed resolution:</b></p>
<p><i>From Function objects 22.10 <a href="https://wg21.link/function.objects">[function.objects]</a>, Header <code>&lt;functional&gt;</code> 
synopsis, remove:</i></p>
<blockquote>
  <pre>// 20.7.4 result_of:
template &lt;class&gt; class result_of; <i>// undefined</i>
template &lt;class F, class... Args&gt; class result_of&lt;F(ArgTypes...)&gt;;</pre>
</blockquote>

<p><i>Remove Function object return types 99 [func.ret] in its entirety. 
This sub-section reads:</i></p>
<blockquote>
  <pre>namespace std {
  template &lt;class&gt; class result_of; <i>// undefined</i>

  template &lt;class Fn, class... ArgTypes&gt;
  class result_of&lt;Fn(ArgTypes...)&gt; {
  public :
    // types
    typedef see below type;
  };
}</pre>
  <p>Given an rvalue <code>fn</code> of type <code>Fn</code> and values <code>
  t1, t2, ..., tN</code> of types T1, T2, ..., TN in <code>ArgTypes</code>, 
  respectively, the type member is the result type of the expression <code>
  fn(t1, t2, ...,tN)</code>. The values <code>ti</code> are lvalues when the 
  corresponding type <code>Ti</code> is an lvalue-reference type, and rvalues 
  otherwise.</p>
</blockquote>
<p><i>To Header &lt;type_traits&gt; synopsis 21.3.3 <a href="https://wg21.link/meta.type.synop">[meta.type.synop]</a>, add at 
the indicated location:</i></p>
<blockquote>
  <pre>template &lt;class T&gt; struct underlying_type;
<ins>template &lt;class T&gt; struct result_of; <i>// not defined
</i>template &lt;class Fn, class... ArgTypes&gt; struct result_of&lt;Fn(ArgTypes...)&gt;;</ins></pre>
</blockquote>
<p><i>To Other transformations 21.3.9.7 <a href="https://wg21.link/meta.trans.other">[meta.trans.other]</a>, Table 51 &mdash; 
Other transformations, add:</i></p>
<blockquote>
  <table style="border-collapse: collapse;" border="1" cellpadding="3" cellspacing="0">
    <tr>
      <td><b>Template</b></td>
      <td><b>Condition</b></td>
      <td><b>Comments</b></td>
    </tr>
    <tr>
      <td valign="top"><code>template &lt;class T&gt;<br/>
      struct underlying_type;</code></td>
      <td valign="top"><code>T</code> shall be an enumeration type 
      (7.2)</td>
      <td valign="top">The member typedef <code>type</code> shall 
      name the underlying type of <code>T</code>.</td>
    </tr>
    <tr>
      <td valign="top"><ins><code>template &lt;class Fn, class... ArgTypes&gt;
      struct result_of&lt;Fn(ArgTypes...)&gt;;</code></ins></td>
      <td valign="top"><ins><code>Fn</code> shall be a function object type
      22.10 <a href="https://wg21.link/function.objects">[function.objects]</a>, reference to function, or reference to
      function object type.
      <code>decltype(declval&lt;Fn&gt;()(declval&lt;ArgTypes&gt;()...))</code> shall
      be well formed.</ins></td>
      <td valign="top"><ins>The member typedef <code>type</code> 
      shall name the type <code>decltype(declval&lt;Fn&gt;()(declval&lt;ArgTypes&gt;()...))</code>.</ins></td>
    </tr>
  </table>
</blockquote>
<p>At the end of Other transformations 21.3.9.7 <a href="https://wg21.link/meta.trans.other">[meta.trans.other]</a> add:</p>

<blockquote>
<p>[<i>Example:</i> Given these definitions:</p>

  <pre>typedef bool(&amp;PF1)();
typedef short(*PF2)(long);

struct S {
&nbsp; operator PF2() const;
&nbsp; double operator()(char, int&amp;);
 };</pre>
<p>the following assertions will hold:</p>
  <pre>static_assert(std::is_same&lt;std::result_of&lt;S(int)&gt;::type, short&gt;::value, &quot;Error!&quot;);
static_assert(std::is_same&lt;std::result_of&lt;S&amp;(unsigned char, int&amp;)&gt;::type, double&gt;::value, &quot;Error!&quot;);
static_assert(std::is_same&lt;std::result_of&lt;PF1()&gt;::type, bool&gt;::value, &quot;Error!&quot;);</pre>
  <p><i>&nbsp;&mdash; end example</i>]</p>
</blockquote>





</body>
</html>
