<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Issue 1054: forward broken</title>
<meta property="og:title" content="Issue 1054: forward broken">
<meta property="og:description" content="C++ library issue. Status: Resolved">
<meta property="og:url" content="https://cplusplus.github.io/LWG/issue1054.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="1054"><a href="lwg-defects.html#1054">1054</a>. <code>forward</code> broken</h3>
<p><b>Section:</b> 22.2.4 <a href="https://wg21.link/forward">[forward]</a> <b>Status:</b> <a href="lwg-active.html#Resolved">Resolved</a>
 <b>Submitter:</b> Howard Hinnant <b>Opened:</b> 2009-03-13 <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#forward">issues</a> in [forward].</p>
<p><b>View all issues with</b> <a href="lwg-status.html#Resolved">Resolved</a> status.</p>
<p><b>Discussion:</b></p>

<p>
This is a placeholder issue to track the fact that we (well I) put the standard
into an inconsistent state by requesting that we accept
<a href="http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2009/n2844.html">N2844</a>
except for the proposed changes to [forward].
</p>

<p>
There will exist in the post meeting mailing
<a href="http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2009/n2835.html">N2835</a>
which in its current state reflects the state of affairs prior to the Summit
meeting.  I hope to update it in time for the post Summit mailing, but as I write
this issue I have not done so yet.
</p>

<p><i>[
Batavia (2009-05):
]</i></p>


<blockquote><p>
Move to Open, awaiting the promised paper.
</p></blockquote>

<p><i>[
2009-08-02 Howard adds:
]</i></p>


<blockquote>
<p>
My current preferred solution is:
</p>

<blockquote><pre>
template &lt;class T&gt;
struct __base_type
{
   typedef typename remove_cv&lt;typename remove_reference&lt;T&gt;::type&gt;::type type;
};

template &lt;class T, class U,
   class = typename enable_if&lt;
       !is_lvalue_reference&lt;T&gt;::value ||
        is_lvalue_reference&lt;T&gt;::value &amp;&amp;
        is_lvalue_reference&lt;U&gt;::value&gt;::type,
   class = typename enable_if&lt;
        is_same&lt;typename __base_type&lt;T&gt;::type,
                typename __base_type&lt;U&gt;::type&gt;::value&gt;::type&gt;
inline
T&amp;&amp;
forward(U&amp;&amp; t)
{
   return static_cast&lt;T&amp;&amp;&gt;(t);
}
</pre></blockquote>

<p>
This has been tested by Bill, Jason and myself.
</p>

<p>
It allows the following lvalue/rvalue casts:
</p>

<ol>
<li>
Cast an lvalue <code>t</code> to an lvalue <code>T</code> (identity).
</li>
<li>
Cast an lvalue <code>t</code> to an rvalue <code>T</code>.
</li>
<li>
Cast an rvalue <code>t</code> to an rvalue <code>T</code> (identity).
</li>
</ol>

<p>
It disallows:
</p>

<ol style="list-style-type:lower-alpha">
<li>
Cast an rvalue <code>t</code> to an lvalue <code>T</code>.
</li>
<li>
Cast one type <code>t</code> to another type <code>T</code> (such as <code>int</code> to <code>double</code>).
</li>
</ol>

<p>
"a." is disallowed as it can easily lead to dangling references.
"b." is disallowed as this function is meant to only change the lvalue/rvalue
characteristic of an expression.
</p>

<p>
Jason has expressed concern that "b." is not dangerous and is useful in contexts
where you want to "forward" a derived type as a base type.  I find this use case
neither dangerous, nor compelling.  I.e. I could live with or without the "b."
constraint.  Without it, forward would look like:
</p>

<blockquote><pre>
template &lt;class T, class U,
   class = typename enable_if&lt;
       !is_lvalue_reference&lt;T&gt;::value ||
        is_lvalue_reference&lt;T&gt;::value &amp;&amp;
        is_lvalue_reference&lt;U&gt;::value&gt;::type&gt;
inline
T&amp;&amp;
forward(U&amp;&amp; t)
{
   return static_cast&lt;T&amp;&amp;&gt;(t);
}
</pre></blockquote>

<p>
Or possibly:
</p>

<blockquote><pre>
template &lt;class T, class U,
   class = typename enable_if&lt;
       !is_lvalue_reference&lt;T&gt;::value ||
        is_lvalue_reference&lt;T&gt;::value &amp;&amp;
        is_lvalue_reference&lt;U&gt;::value&gt;::type,
   class = typename enable_if&lt;
        is_base_of&lt;typename __base_type&lt;U&gt;::type,
                   typename __base_type&lt;T&gt;::type&gt;::value&gt;::type&gt;
inline
T&amp;&amp;
forward(U&amp;&amp; t)
{
   return static_cast&lt;T&amp;&amp;&gt;(t);
}
</pre></blockquote>


<p>
The "promised paper" is not in the post-Frankfurt mailing only because I'm waiting
for the non-concepts draft.  But I'm hoping that by adding this information here
I can keep people up to date.
</p>
</blockquote>

<p><i>[
2009-08-02 David adds:
]</i></p>


<blockquote>
<p>
<code>forward</code> was originally designed to do one thing: perfect forwarding.
That is, inside a function template whose actual argument can be a const
or non-const lvalue or rvalue, restore the original "rvalue-ness" of the
actual argument:
</p>

<blockquote><pre>
template &lt;class T&gt;
void f(T&amp;&amp; x)
{
    // x is an lvalue here.  If the actual argument to f was an
    // rvalue, pass static_cast&lt;T&amp;&amp;&gt;(x) to g; otherwise, pass x.
    g( forward&lt;T&gt;(x) );
}
</pre></blockquote>

<p>
Attempting to engineer <code>forward</code> to accomodate uses other than perfect
forwarding dilutes its idiomatic meaning.  The solution proposed here
declares that <code>forward&lt;T&gt;(x)</code> means nothing more than <code>static_cast&lt;T&amp;&amp;&gt;(x)</code>,
with a patchwork of restrictions on what <code>T</code> and <code>x</code> can be that can't be
expressed in simple English.
</p>

<p>
I would be happy with either of two approaches, whose code I hope (but
can't guarantee) I got right.
</p>

<ol>
<li>
<p>
Use a simple definition of <code>forward</code> that accomplishes its original
purpose without complications to accomodate other uses:
</p>

<blockquote><pre>
template &lt;class T, class U&gt;
T&amp;&amp; forward(U&amp; x)
{
    return static_cast&lt;T&amp;&amp;&gt;(x);
}
</pre></blockquote>
</li>

<li>
<p>
Use a definition of <code>forward</code> that protects the user from as many
potential mistakes as possible, by actively preventing <em>all</em> other
uses:
</p>

<blockquote><pre>
template &lt;class T, class U&gt;
boost::enable_if_c&lt;
    // in forward&lt;T&gt;(x), x is a parameter of the caller, thus an lvalue
    is_lvalue_reference&lt;U&gt;::value
    // in caller's deduced T&amp;&amp; argument, T can only be non-ref or lvalue ref
    &amp;&amp; !is_rvalue_reference&lt;T&gt;::value
    // Must not cast cv-qualifications or do any type conversions
    &amp;&amp; is_same&lt;T&amp;,U&amp;&gt;::value
    , T&amp;&amp;&gt;::type forward(U&amp;&amp; a)
{
    return static_cast&lt;T&amp;&amp;&gt;(a);
}
</pre></blockquote>
</li>
</ol>

</blockquote>

<p><i>[
2009-09-27 Howard adds:
]</i></p>


<blockquote><p>
A paper,
<a href="http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2009/n2951.html">N2951</a>,
is available which compares several implementations (including David's) with respect to several
use cases (including Jason's) and provides wording for one implementation.
</p></blockquote>

<p><i>[
2009-10 Santa Cruz:
]</i></p>


<blockquote><p>
<del>NAD Editorial</del><ins>Resolved</ins>.  Solved by
<a href="http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2009/n2951.html">N2951</a>.
</p></blockquote>



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





</body>
</html>
