<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Issue 939: Problem with std::identity and reference-to-temporaries</title>
<meta property="og:title" content="Issue 939: Problem with std::identity and reference-to-temporaries">
<meta property="og:description" content="C++ library issue. Status: C++11">
<meta property="og:url" content="https://cplusplus.github.io/LWG/issue939.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="939"><a href="lwg-defects.html#939">939</a>. Problem with <code>std::identity</code> and reference-to-temporaries</h3>
<p><b>Section:</b> 22.2.4 <a href="https://wg21.link/forward">[forward]</a> <b>Status:</b> <a href="lwg-active.html#C++11">C++11</a>
 <b>Submitter:</b> Alisdair Meredith <b>Opened:</b> 2008-12-11 <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#C++11">C++11</a> status.</p>
<p><b>Discussion:</b></p>
<p>
<code>std::identity</code> takes an argument of type <code>T const &amp;</code>
and returns a result of <code>T const &amp;</code>.
</p>
<p>
Unfortunately, this signature will accept a value of type other than <code>T</code> that
is convertible-to-<code>T</code>, and then return a reference to the dead temporary.  The
constraint in the concepts version simply protects against returning
reference-to-<code>void</code>.
</p>
<p>
Solutions:
</p>
<blockquote>
<p>
i/  Return-by-value, potentially slicing bases and rejecting non-copyable
types
</p>
<p>
ii/ Provide an additional overload:
</p>
<blockquote><pre>
template&lt; typename T &gt;
template operator( U &amp; ) = delete;
</pre></blockquote>
<p>
This seems closer on intent, but moves beyond the original motivation for
the operator, which is compatibility with existing (non-standard)
implementations.
</p>
<p>
iii/ Remove the <code>operator()</code> overload.  This restores the original definition
of the <code>identity</code>, although now effectively a type_trait rather than part of
the perfect forwarding protocol.
</p>
<p>
iv/ Remove <code>std::identity</code> completely; its original reason to exist is
replaced with the <code>IdentityOf</code> concept.
</p>
</blockquote>
<p>
My own preference is somewhere between (ii) and (iii) - although I stumbled
over the issue with a specific application hoping for resolution (i)!
</p>

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

<blockquote>
<p>
We dislike options i and iii, and option ii seems like overkill.
If we remove it (option iv), implementers can still provide it under a
different name.
</p>
<p>
Move to Open pending wording (from Alisdair) for option iv.
</p>
</blockquote>

<p><i>[
2009-05-23 Alisdair provided wording for option iv.
]</i></p>


<p><i>[
2009-07-20 Alisdair adds:
]</i></p>


<blockquote>
<p>
I'm not sure why this issue was not discussed at Frankfurt (or I missed
the discussion) but the rationale is now fundamentally flawed.  With the
removal of concepts, <code>std::identity</code> again becomes an important library
type so we cannot simply remove it.
</p>
<p>
At that point, we need to pick one of the other suggested resolutions,
but have no guidance at the moment.
</p>
</blockquote>

<p><i>[
2009-07-20 Howard adds:
]</i></p>


<blockquote>
<p>
I believe the rationale for not addressing this issue in Frankfurt was that it did
not address a national body comment.
</p>
<p>
I also believe that removal of <code>identity</code> is still a practical option as
my latest reformulation of <code>forward</code>, which is due to comments suggested
at Summit, no longer uses <code>identity</code>. :-)
</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_same&lt;typename remove_all&lt;T&gt;::type,
                        typename remove_all&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>

<p><i>[
The above code assumes acceptance of <a href="lwg-closed.html#1120" title="New type trait - remove_all (Status: NAD)">1120</a><sup><a href="https://cplusplus.github.io/LWG/issue1120" title="Latest snapshot">(i)</a></sup> for the definition of
<code>remove_all</code>.  This is just to make the syntax a little more palatable.
Without this trait the above is still very implementable.
]</i></p>


</blockquote>

<p>
Paper with rationale is on the way ... <i>really</i>, I promise this time! ;-)
</p>
</blockquote>

<p><i>[
2009-07-30 Daniel adds:  See <a href="lwg-defects.html#823" title="identity&lt;void&gt; seems broken (Status: Resolved)">823</a><sup><a href="https://cplusplus.github.io/LWG/issue823" title="Latest snapshot">(i)</a></sup> for an alternative resolution.
]</i></p>


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


<blockquote><p>
Move to Ready. Howard will update proposed wording to reflect current draft.
</p></blockquote>



<p id="res-939"><b>Proposed resolution:</b></p>
<p>
Strike from 22.2 <a href="https://wg21.link/utility">[utility]</a>:
</p>

<blockquote><pre>
<del>template &lt;class T&gt; struct identity;</del>
</pre></blockquote>

<p>
Remove from 22.2.4 <a href="https://wg21.link/forward">[forward]</a>:
</p>

<blockquote>
<pre>
<del>template &lt;class T&gt; struct identity {
  typedef T type;

  const T&amp; operator()(const T&amp; x) const;
};</del>

<del>const T&amp; operator()(const T&amp; x) const;</del>
</pre>
<blockquote><p>
<del>-2-  <i>Returns:</i> <code>x</code></del>
</p></blockquote>
</blockquote>






</body>
</html>
