<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Issue 3407: Some problems with the wording changes of P1739R4</title>
<meta property="og:title" content="Issue 3407: Some problems with the wording changes of P1739R4">
<meta property="og:description" content="C++ library issue. Status: C++23">
<meta property="og:url" content="https://cplusplus.github.io/LWG/issue3407.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++23">C++23</a> status.</em></p>
<h3 id="3407"><a href="lwg-defects.html#3407">3407</a>. Some problems with the wording changes of <a href="https://wg21.link/p1739r4">P1739R4</a></h3>
<p><b>Section:</b> 25.7.10.1 <a href="https://wg21.link/range.take.overview">[range.take.overview]</a>, 25.6.4 <a href="https://wg21.link/range.iota">[range.iota]</a> <b>Status:</b> <a href="lwg-active.html#C++23">C++23</a>
 <b>Submitter:</b> Patrick Palka <b>Opened:</b> 2020-02-21 <b>Last modified:</b> 2023-11-22</p>
<p><b>Priority: </b>2
</p>
<p><b>View other</b> <a href="lwg-index-open.html#range.take.overview">active issues</a> in [range.take.overview].</p>
<p><b>View all other</b> <a href="lwg-index.html#range.take.overview">issues</a> in [range.take.overview].</p>
<p><b>View all issues with</b> <a href="lwg-status.html#C++23">C++23</a> status.</p>
<p><b>Discussion:</b></p>
<p>
Section 6.1 of <a href="https://wg21.link/p1739r4">P1739R4</a> changes the specification of
<code>views::take</code> as follows:
</p>
<blockquote>
<p>
-2- The name <code>views::take</code> denotes a range adaptor object (25.7.2 <a href="https://wg21.link/range.adaptor.object">[range.adaptor.object]</a>).
<del>Given subexpressions <code>E</code> and <code>F</code>, the expression <code>views::take(E, F)</code> is
expression-equivalent to <code>take_view{E, F}</code>.</del>
<ins>Let <code>E</code> and <code>F</code> be expressions, let <code>T</code> be <code>remove_cvref_t&lt;decltype((E))&gt;</code>,
and let <code>D</code> be <code>range_difference_t&lt;decltype((E))&gt;</code>.
If <code>decltype((F))</code> does not model <code>convertible_to&lt;D&gt;</code>, <code>views::take(E, F)</code>
is ill-formed. Otherwise, the expression <code>views::take(E, F)</code> is expression-equivalent to:</ins>
</p>
<ol style="list-style-type: none">
<li><p><ins>&mdash; if <code>T</code> is a specialization of <code>ranges::empty_view</code> (25.6.2.2 <a href="https://wg21.link/range.empty.view">[range.empty.view]</a>),
then <code>((void) F, <i>decay-copy</i>(E))</code>;</ins>
</p></li>
<li><p><ins>&mdash; otherwise, if <code>T</code> models <code>random_access_range</code> and <code>sized_range</code> and
is</ins>
</p>
<ol style="list-style-type: none">
<li><p><ins>&mdash; a specialization of <code>span</code> (23.7.2.2 <a href="https://wg21.link/views.span">[views.span]</a>) where
<code>T::extent == dynamic_extent</code>,</ins></p></li>
<li><p><ins>&mdash; a specialization of <code>basic_string_view</code> (27.3 <a href="https://wg21.link/string.view">[string.view]</a>),</ins></p></li>
<li><p><ins>&mdash; a specialization of <code>ranges::iota_view</code> (25.6.4.2 <a href="https://wg21.link/range.iota.view">[range.iota.view]</a>), or</ins></p></li>
<li><p><ins>&mdash; a specialization of <code>ranges::subrange</code> (25.5.4 <a href="https://wg21.link/range.subrange">[range.subrange]</a>),</ins></p></li>
</ol>
<p><ins>then <code>T{ranges::begin(E), ranges::begin(E) + min&lt;D&gt;(ranges::size(E), F)}</code>,
except that <code>E</code> is evaluated only once;</ins>
</p></li>
<li><p>
<ins>&mdash; otherwise, <code>ranges::take_view{E, F}</code>.</ins>
</p></li>
</ol>
</blockquote>
<p>
Consider the case when <code>T = subrange&lt;counted_iterator&lt;int&gt;, default_sentinel_t&gt;</code>.
Then according to the above wording, <code>views::take(E, F)</code> is expression-equivalent to
</p>
<blockquote><pre>
T{ranges::begin(E), ranges:begin(E) + min&lt;D&gt;(ranges::size(E), F)};   <i>(*)</i>
</pre></blockquote>
<p>
But this expression is ill-formed for the <code>T</code> we chose because
<code>subrange&lt;counted_iterator&lt;int&gt;, default_sentinel_t&gt;</code> has no matching
constructor that takes an <code>iterator-iterator</code> pair.
<p/>
More generally the above issue applies anytime <code>T</code> is a specialization of
<code>subrange</code> that does not model <code>common_range</code>. But a similar issue also
exists when <code>T</code> is a specialization of <code>iota_view</code> whose value type differs
from its bound type. In this case yet another issue arises: In order for the expression
<code><i>(*)</i></code> to be well-formed when <code>T</code> is a specialization of <code>iota_view</code>,
we need to be able to construct an <code>iota_view</code> out of an <code>iterator-iterator</code> pair,
and for that it seems we need to add another constructor to <code>iota_view</code>.
</p>

<p><i>[2020-02-24, Casey comments]</i></p>

<p>
Furthermore, the pertinent subrange constructor is only available when <code>subrange::StoreSize</code> is
<code>false</code> (i.e., when either the <code>subrange</code> specialization's third template argument is
not <code>subrange_kind::sized</code> or its iterator and sentinel types <code>I</code> and <code>S</code> model
<code>sized_sentinel_for&lt;S, I&gt;</code>).
</p>

<p><i>[2020-03-16, Tomasz comments]</i></p>

<p>
A similar problem occurs for the <code>views::drop</code> for the <code>subrange&lt;I, S, subrange_kind::sized&gt;</code>,
that explicitly stores size (i.e. <code>sized_sentinel_for&lt;I, S&gt;</code> is <code>false</code>). In such case,
the <code>(iterator, sentinel)</code> constructor that <code>views::drop</code> will be expression-equivalent is
not available.
</p>

<p><i>[2020-03-29 Issue Prioritization]</i></p>

<p>Priority to 2 after reflector discussion.</p>

<p><i>[2021-05-18 Tim adds wording]</i></p>

<p>The proposed resolution below is based on the MSVC implementation, with one caveat:
the MSVC implementation uses a SCARY iterator type for <code>iota_view</code> and therefore
its <code>iota_view</code> case for <code>take</code> is able to directly construct the new view
from the iterator type of the original. This is not required to work, so the wording below
constructs the <code>iota_view</code> from the result of dereferencing the iterators instead
in this case.
</p>

<p><i>[2021-06-18 Tim syncs wording to the current working draft]</i></p>

<p>The wording below also corrects the size calculation in the presence of
integer-class types.</p>

<p><i>[2021-08-20; LWG telecon]</i></p>

<p>
Set status to Tentatively Ready after telecon review.
</p>

<p><i>[2021-10-14 Approved at October 2021 virtual plenary. Status changed: Voting &rarr; WP.]</i></p>



<p id="res-3407"><b>Proposed resolution:</b></p>
<p>This wording is relative to <a href="https://wg21.link/N4892" title=" Working Draft, Standard for Programming Language C++">N4892</a>.</p>
<ol>
<li>
<p>Edit 25.7.10.1 <a href="https://wg21.link/range.take.overview">[range.take.overview]</a> as indicated: </p>
<blockquote>
<p>
-2- The name <code>views::take</code> denotes a range adaptor object (25.7.2 <a href="https://wg21.link/range.adaptor.object">[range.adaptor.object]</a>).
Let <code>E</code> and <code>F</code> be expressions, let <code>T</code> be <code>remove_cvref_t&lt;decltype((E))&gt;</code>,
and let <code>D</code> be <code>range_difference_t&lt;decltype((E))&gt;</code>.
If <code>decltype((F))</code> does not model <code>convertible_to&lt;D&gt;</code>, <code>views::take(E, F)</code>
is ill-formed. Otherwise, the expression <code>views::take(E, F)</code> is expression-equivalent to:
</p>
<ol style="list-style-type: none">
<li><p>(2.1) &mdash; if <code>T</code> is a specialization of <code>ranges::empty_view</code> (25.6.2.2 <a href="https://wg21.link/range.empty.view">[range.empty.view]</a>),
then <code>((void) F, <i>decay-copy</i>(E))</code>, except that the evaluations of <code>E</code>
and <code>F</code> are indeterminately sequenced;
</p></li>
<li><p>(2.2) &mdash; otherwise, if <code>T</code> models <code>random_access_range</code> and <code>sized_range</code>,
and is <ins>a specialization of <code>span</code> (23.7.2.2 <a href="https://wg21.link/views.span">[views.span]</a>), <code>basic_string_view</code> (27.3 <a href="https://wg21.link/string.view">[string.view]</a>),
or <code>ranges::subrange</code> (25.5.4 <a href="https://wg21.link/range.subrange">[range.subrange]</a>),
then <code>U(ranges::begin(E), ranges::begin(E) + std::min&lt;D&gt;(ranges::distance(E), F))</code>,
except that <code>E</code> is evaluated only once, where <code>U</code> is a type determined as follows:</ins>
</p>
<ol style="list-style-type: none">
<li><p>(2.2.1) &mdash; <ins>if <code>T</code> is</ins> a specialization of <code>span</code> <del>(23.7.2.2 <a href="https://wg21.link/views.span">[views.span]</a>) where
<code>T::extent == dynamic_extent</code></del>,<ins> then <code>U</code> is <code>span&lt;typename T::element_type&gt;</code>;</ins></p></li>
<li><p>(2.2.2) &mdash; <ins>otherwise, if <code>T</code> is</ins> a specialization of <code>basic_string_view</code> <del>(27.3 <a href="https://wg21.link/string.view">[string.view]</a>)</del>, <ins>then <code>U</code> is <code>T</code>;</ins></p></li>
<li><p><del>(2.2.3) &mdash; a specialization of <code>ranges::iota_view</code> (25.6.4.2 <a href="https://wg21.link/range.iota.view">[range.iota.view]</a>), or</del></p></li>
<li><p>(2.2.4) &mdash; <ins>otherwise, <code>T</code> is</ins> a specialization of <code>ranges::subrange</code> <del>(25.5.4 <a href="https://wg21.link/range.subrange">[range.subrange]</a>)</del>,
<ins>and <code>U</code> is <code>ranges::subrange&lt;iterator_t&lt;T&gt;&gt;</code>;</ins></p></li>
</ol>
<p><del>then <code>T{ranges::begin(E), ranges::begin(E) + min&lt;D&gt;(ranges::size(E), F)}</code>,
except that <code>E</code> is evaluated only once;</del>
</p></li>
<li><p><ins>(2.?) &mdash; otherwise, if <code>T</code> is a specialization of <code>ranges::iota_view</code> (25.6.4.2 <a href="https://wg21.link/range.iota.view">[range.iota.view]</a>)
that models <code>random_access_range</code> and <code>sized_range</code>, then
<code>ranges::iota_view(*ranges::begin(E), *(ranges::begin(E) + std::min&lt;D&gt;(ranges::distance(E), F)))</code>,
except that <code>E</code> and <code>F</code> are each evaluated only once;</ins></p></li>
<li><p>(2.3) &mdash; otherwise, <code>ranges::take_view(E, F)</code>.
</p></li>
</ol>
</blockquote>
</li>
<li>
<p>Edit 25.7.12.1 <a href="https://wg21.link/range.drop.overview">[range.drop.overview]</a> as indicated: </p>
<blockquote>
<p>
-2- The name <code>views::drop</code> denotes a range adaptor object (25.7.2 <a href="https://wg21.link/range.adaptor.object">[range.adaptor.object]</a>).
Let <code>E</code> and <code>F</code> be expressions, let <code>T</code> be <code>remove_cvref_t&lt;decltype((E))&gt;</code>,
and let <code>D</code> be <code>range_difference_t&lt;decltype((E))&gt;</code>.
If <code>decltype((F))</code> does not model <code>convertible_to&lt;D&gt;</code>, <code>views::drop(E, F)</code>
is ill-formed. Otherwise, the expression <code>views::drop(E, F)</code> is expression-equivalent to:
</p>
<ol style="list-style-type: none">
<li><p>(2.1) &mdash; if <code>T</code> is a specialization of <code>ranges::empty_view</code> (25.6.2.2 <a href="https://wg21.link/range.empty.view">[range.empty.view]</a>),
then <code>((void) F, <i>decay-copy</i>(E))</code>, except that the evaluations of <code>E</code>
and <code>F</code> are indeterminately sequenced;
</p></li>
<li><p>(2.2) &mdash; otherwise, if <code>T</code> models <code>random_access_range</code> and <code>sized_range</code>,
and is
</p>
<ol style="list-style-type: none">
<li><p>(2.2.1) &mdash;a specialization of <code>span</code> (23.7.2.2 <a href="https://wg21.link/views.span">[views.span]</a>) <del>where
<code>T::extent == dynamic_extent</code></del>,</p></li>
<li><p>(2.2.2) &mdash; a specialization of <code>basic_string_view</code> (27.3 <a href="https://wg21.link/string.view">[string.view]</a>), </p></li>
<li><p>(2.2.3) &mdash; a specialization of <code>ranges::iota_view</code> (25.6.4.2 <a href="https://wg21.link/range.iota.view">[range.iota.view]</a>), or</p></li>
<li><p>(2.2.4) &mdash; a specialization of <code>ranges::subrange</code> (25.5.4 <a href="https://wg21.link/range.subrange">[range.subrange]</a>)
<ins>where <code>T::<i>StoreSize</i></code> is <code>false</code></ins>,</p></li>
</ol>
<p>then <code><del>T</del><ins>U</ins>(ranges::begin(E) + <ins>std::</ins>min&lt;D&gt;(ranges::<del>size</del><ins>distance</ins>(E), F), ranges::end(E))</code>,
except that <code>E</code> is evaluated only once<ins>, where <code>U</code> is <code>span&lt;typename T::element_type&gt;</code>
if <code>T</code> is a specialization of <code>span</code> and <code>T</code> otherwise</ins>;
</p></li>
<li><p><ins>(2.?) &mdash; otherwise, if <code>T</code> is a specialization of <code>ranges::subrange</code>
(25.5.4 <a href="https://wg21.link/range.subrange">[range.subrange]</a>) that models <code>random_access_range</code> and <code>sized_range</code>, then
<code>T(ranges::begin(E) + std::min&lt;D&gt;(ranges::distance(E), F), ranges::end(E),
<i>to-unsigned-like</i>(ranges::distance(E) - std::min&lt;D&gt;(ranges::distance(E), F)))</code>,
except that <code>E</code> and <code>F</code> are each evaluated only once;</ins></p></li>
<li><p>(2.3) &mdash; otherwise, <code>ranges::drop_view(E, F)</code>.
</p></li>
</ol>
</blockquote>
</li>
</ol>




</body>
</html>
