<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Issue 2314: apply() should return decltype(auto) and use decay_t before tuple_size</title>
<meta property="og:title" content="Issue 2314: apply() should return decltype(auto) and use decay_t before tuple_size">
<meta property="og:description" content="C++ library issue. Status: C++14">
<meta property="og:url" content="https://cplusplus.github.io/LWG/issue2314.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++14">C++14</a> status.</em></p>
<h3 id="2314"><a href="lwg-defects.html#2314">2314</a>. <code>apply()</code> should return <code>decltype(auto)</code> and use <code>decay_t</code> before <code>tuple_size</code></h3>
<p><b>Section:</b> 21.2.1 <a href="https://wg21.link/intseq.general">[intseq.general]</a> <b>Status:</b> <a href="lwg-active.html#C++14">C++14</a>
 <b>Submitter:</b> Stephan T. Lavavej <b>Opened:</b> 2013-09-21 <b>Last modified:</b> 2017-07-05</p>
<p><b>Priority: </b>0
</p>
<p><b>View all issues with</b> <a href="lwg-status.html#C++14">C++14</a> status.</p>
<p><b>Discussion:</b></p>
<p>
The example in 21.2.1 <a href="https://wg21.link/intseq.general">[intseq.general]</a>/2 depicts <code>apply_impl()</code> and <code>apply()</code> as returning <code>auto</code>.  
This is incorrect because it will trigger decay and will not preserve <code>F</code>'s return type. For example, if invoking the 
functor returns <code>const int&amp;</code>, <code>apply_impl()</code> and <code>apply()</code> will return <code>int</code>. <code>decltype(auto)</code> 
should be used for "perfect returning".
</p>
<p>
Additionally, this depicts <code>apply()</code> as taking <code>Tuple&amp;&amp;</code>, then saying "<code>std::tuple_size&lt;Tuple&gt;::value</code>". 
This is incorrect because when <code>apply()</code> is called with lvalue tuples, perfect forwarding will deduce <code>Tuple</code> to be <i>cv</i> 
<code>tuple&amp;</code>, but 22.4.7 <a href="https://wg21.link/tuple.helper">[tuple.helper]</a> says that <code>tuple_size</code> handles only <i>cv</i> <code>tuple</code>, not 
references to tuples.  Using <code>remove_reference_t</code> would avoid this problem, but so would <code>decay_t</code>, which has a 
significantly shorter name. (The additional transformations that <code>decay_t</code> does are neither beneficial nor harmful here.)
</p>

<p><i>[Issaquah 2014-02-11: Move to Immediate]</i></p>




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

<ol>
<li><p>Edit the example code in 21.2.1 <a href="https://wg21.link/intseq.general">[intseq.general]</a>/2 as indicated:</p>

<blockquote><pre>
template&lt;class F, class Tuple, std::size_t... I&gt;
  <del>auto</del><ins>decltype(auto)</ins> apply_impl(F&amp;&amp; f, Tuple&amp;&amp; t, index_sequence&lt;I...&gt;) {
    return std::forward&lt;F&gt;(f)(std::get&lt;I&gt;(std::forward&lt;Tuple&gt;(t))...);
  }
template&lt;class F, class Tuple&gt;
  <del>auto</del><ins>decltype(auto)</ins> apply(F&amp;&amp; f, Tuple&amp;&amp; t) {
    using Indices = make_index_sequence&lt;std::tuple_size&lt;<ins>std::decay_t&lt;</ins>Tuple<ins>&gt;</ins>&gt;::value&gt;;
    return apply_impl(std::forward&lt;F&gt;(f), std::forward&lt;Tuple&gt;(t), Indices());
  }
</pre></blockquote>
</li>

</ol>





</body>
</html>
