<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Issue 3996: projected&lt;I, identity&gt; should just be I</title>
<meta property="og:title" content="Issue 3996: projected&lt;I, identity&gt; should just be I">
<meta property="og:description" content="C++ library issue. Status: NAD">
<meta property="og:url" content="https://cplusplus.github.io/LWG/issue3996.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#NAD">NAD</a> status.</em></p>
<h3 id="3996"><a href="lwg-closed.html#3996">3996</a>. <code>projected&lt;I, identity&gt;</code> should just be <code>I</code></h3>
<p><b>Section:</b> 24.3.6.4 <a href="https://wg21.link/projected">[projected]</a> <b>Status:</b> <a href="lwg-active.html#NAD">NAD</a>
 <b>Submitter:</b> Hewill Kang <b>Opened:</b> 2023-10-12 <b>Last modified:</b> 2024-06-24</p>
<p><b>Priority: </b>Not Prioritized
</p>
<p><b>View all other</b> <a href="lwg-index.html#projected">issues</a> in [projected].</p>
<p><b>View all issues with</b> <a href="lwg-status.html#NAD">NAD</a> status.</p>
<p><b>Discussion:</b></p>
<p>
Currently, <code>projected</code> is a wrapper of the implementation type regardless of whether <code>Proj</code> is <code>identity</code>.
<p/>
Since <code>identity</code> always returns a reference, this prevents <code>projected&lt;I, identity&gt;</code> from fully 
emulating the properties of the original iterator when its reference is a prvalue. 
<p/>
Such non-equivalence may lead to unexpected behavior in some cases (<a href="https://godbolt.org/z/KM45ndWvh">demo</a>):
</p>
<blockquote><pre>
#include &lt;algorithm&gt;
#include &lt;ranges&gt;
#include &lt;iostream&gt;

int main() {
  auto outer = std::views::iota(0, 5)
             | std::views::transform([](int i) {
                 return std::views::single(i) | std::views::filter([](int) { return true; });
               });
  
  for (auto&amp;&amp; inner : outer)
    for (auto&amp;&amp; elem : inner)
      std::cout &lt;&lt; elem &lt;&lt; " "; // 0 1 2 3 4 
  
  std::ranges::for_each(
    outer,
    [](auto&amp;&amp; inner) {
      // <span  style="color:#C80000;font-weight:bold">error: passing 'const filter_view' as 'this' argument discards qualifiers</span>
      for (auto&amp;&amp; elem : inner)
        std::cout &lt;&lt; elem &lt;&lt; " ";
    });
}
</pre></blockquote>
<p>
In the above example, <code>ranges::for_each</code> requires <code>indirect_unary_predicate&lt;Pred, projected&lt;I, identity&gt;&gt;</code>
which ultimately requires <code>invocable&lt;Pred&amp;, iter_common_reference_t&lt;projected&lt;I, identity&gt;&gt;&gt;</code>.
<p/> 
According to the current wording, the reference and indirect value type of <code>projected&lt;I, identity&gt;</code> are 
<code>filter_view&amp;&amp;</code> and <code>filter_view&amp;</code> respectively, which causes its common reference to be eventually 
calculated as <code>const filter_view&amp;</code>. Since the former is not <code>const</code>-iterable, this results in a hard error 
during instantiation because <code>const begin</code> is called unexpectedly in an unconstrained lambda.
</p>
<p>
It seems like having <code>projected&lt;I, identity&gt;</code> just be <code>I</code> is a more appropriate choice, 
which makes the concept checking really specific to <code>I</code> rather than a potentially incomplete iterator wrapper.
</p>

<p><i>[2023-11-03; Reflector poll]</i></p>

<p>
NAD. <a href="https://wg21.link/P2997" title=" Removing the common reference requirement from the indirectly invocable concepts">P2997</a> solves this, and more.
"Applying the projection does in fact materialize prvalues,
so this is just lying unless we special-case identity everywhere."
</p>

<p><i>[St. Louis 2024-06-24 Status changed: Tentatively NAD &rarr; NAD.]</i></p>



<p id="res-3996"><b>Proposed resolution:</b></p>
<p>
This wording is relative to <a href="https://wg21.link/N4958" title=" Working Draft, Programming Languages — C++">N4958</a>.
</p>

<ol>

<li><p>Modify 24.3.6.4 <a href="https://wg21.link/projected">[projected]</a> as indicated:</p>

<blockquote>
<p>
-1- Class template <code>projected</code> is used to constrain algorithms that accept callable objects and projections 
(3.44 <a href="https://wg21.link/defns.projection">[defns.projection]</a>). It combines an <code>indirectly_readable</code> type <code>I</code> and a callable 
object type <code>Proj</code> into a new <code>indirectly_readable</code> type whose <code>reference</code> type is the 
result of applying <code>Proj</code> to the <code>iter_reference_t</code> of <code>I</code>.
</p>
<blockquote><pre>
namespace std {
  template&lt;class I, class Proj&gt;
  struct <i>projected-impl</i> {                               // <i>exposition only</i>
    struct <i>type</i> {                                       // <i>exposition only</i>
      using value_type = remove_cvref_t&lt;indirect_result_t&lt;Proj&amp;, I&gt;&gt;;
      using difference_type = iter_difference_t&lt;I&gt;;     // <i>present only if</i> I
                                                        // <i>models</i> weakly_incrementable
      indirect_result_t&lt;Proj&amp;, I&gt; operator*() const;    // <i>not defined</i>
    };
  };

  template&lt;indirectly_readable I, indirectly_regular_unary_invocable&lt;I&gt; Proj&gt;
    using projected = <ins>conditional_t&lt;is_same_v&lt;Proj, identity&gt;, I, typename</ins> <i>projected-impl</i>&lt;I, Proj&gt;::type<ins>&gt;</ins>;
}
</pre></blockquote>
</blockquote>


</li>

</ol>





</body>
</html>
