<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Issue 3368: Exactly when does size return end - begin?</title>
<meta property="og:title" content="Issue 3368: Exactly when does size return end - begin?">
<meta property="og:description" content="C++ library issue. Status: Resolved">
<meta property="og:url" content="https://cplusplus.github.io/LWG/issue3368.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="3368"><a href="lwg-defects.html#3368">3368</a>. Exactly when does <code>size</code> return <code>end - begin</code>?</h3>
<p><b>Section:</b> 25.3.10 <a href="https://wg21.link/range.prim.size">[range.prim.size]</a> <b>Status:</b> <a href="lwg-active.html#Resolved">Resolved</a>
 <b>Submitter:</b> Casey Carter <b>Opened:</b> 2020-01-07 <b>Last modified:</b> 2021-02-25</p>
<p><b>Priority: </b>0
</p>
<p><b>View all issues with</b> <a href="lwg-status.html#Resolved">Resolved</a> status.</p>
<p><b>Discussion:</b></p>
<p>
The specification of <code>ranges::size</code> in 25.3.10 <a href="https://wg21.link/range.prim.size">[range.prim.size]</a> suggests that bullet 1.3 
("Otherwise, <code><i>make-unsigned-like</i>(ranges::end(E) - ranges::begin(E)) ...")</code> only applies 
when <code>disable_sized_range&lt;remove_cv_t&lt;T&gt;&gt;</code> is <code>true</code>. This is not the 
design intent, but the result of an erroneous attempt to factor out the common 
"<code>disable_sized_range</code> is <code>false</code>" requirement from the member and non-member <code>size</code> 
cases in bullets 1.2.1 and 1.2.2 that occurred between <a href="https://wg21.link/p0896r3">P0896R3</a> and 
<a href="https://wg21.link/p0896r4">P0896R4</a>. The intended design has always been that a range with 
member or non-member <code>size</code> with the same syntax but different semantics may opt-out of being 
sized by specializing <code>disable_sized_range</code>. It has never been intended that arrays or ranges 
whose iterator and sentinel model <code>sized_sentinel_for</code> be able to opt out of being sized via 
<code>disable_sized_range</code>. <code>disable_sized_sentinel_for</code> can/must be used to opt out in the 
latter case so that library functions oblivious to the range type that operate on the iterator and 
sentinel of such a range will avoid subtraction.
</p>

<p><i>[2020-01-25 Status set to Tentatively Ready after six positive votes on the reflector.]</i></p>

<p><i>[2020-11-09 Approved In November virtual meeting. Status changed: Tentatively Ready &rarr; WP.]</i></p>

<p><i>[2020-11-18 This was actually resolved by <a href="https://wg21.link/p2091r0">P2091R0</a> in Prague. Status changed: WP &rarr; Resolved.]</i></p>



<p id="res-3368"><b>Proposed resolution:</b></p>
<p>This wording is relative to <a href="https://wg21.link/n4842">N4842</a>.</p>

<ol>
<li><p>Modify 25.3.10 <a href="https://wg21.link/range.prim.size">[range.prim.size]</a> as indicated:</p>

<blockquote class="note">
<p>
[<i>Drafting note:</i> There are drive-by changes here to (1) avoid introducing unused type placeholders, 
(2) avoid reusing "<code>T</code>" as both the type of the subexpression and the template parameter of the 
poison pill, and (3) fix the cross-reference for <code><i>make-unsigned-like</i></code> which is defined in 
[ranges.syn]/1, not in [range.subrange].]
</p>
</blockquote>

<blockquote>
<p>
-1- The name <code>size</code> denotes a customization point object (16.3.3.3.5 <a href="https://wg21.link/customization.point.object">[customization.point.object]</a>). 
The expression <code>ranges::size(E)</code> for some subexpression <code>E</code> with type <code>T</code> is 
expression-equivalent to:
</p>
<ol style="list-style-type: none">
<li><p>(1.1) &mdash; <code><i>decay-copy</i>(extent_v&lt;T&gt;)</code> if <code>T</code> is an array type 
(6.9.4 <a href="https://wg21.link/basic.compound">[basic.compound]</a>).</p></li>
<li><p><del>(1.2) &mdash; Otherwise, if <code>disable_sized_range&lt;remove_cv_t&lt;T&gt;&gt;</code> 
(25.4.4 <a href="https://wg21.link/range.sized">[range.sized]</a>) is <code>false</code>:</del></p>
<li><p>(1.<ins>?</ins><del>2.1</del>) &mdash; <ins>Otherwise, if 
<code>disable_sized_range&lt;remove_cv_t&lt;T&gt;&gt;</code> (25.4.4 <a href="https://wg21.link/range.sized">[range.sized]</a>) is <code>false</code> 
and</ins> <code><i>decay-copy</i>(E.size())</code> <del>if it</del> is a valid expression <del>and its 
type <code>I</code> is</del><ins>of</ins> integer-like <ins>type</ins> 
(24.3.4.4 <a href="https://wg21.link/iterator.concept.winc">[iterator.concept.winc]</a>)<ins>, <code><i>decay-copy</i>(E.size())</code></ins>.</p></li>
<li><p>(1.<ins>?</ins><del>2.2</del>) &mdash; Otherwise, <ins>if 
<code>disable_sized_range&lt;remove_cv_t&lt;T&gt;&gt;</code> is <code>false</code> and</ins>
<code><i>decay-copy</i>(size(E))</code> <del>if it</del> is a valid expression <del>and its type 
<code>I</code> is</del><ins>of</ins> integer-like <ins>type</ins> with overload resolution performed in a 
context that includes the declaration:
<blockquote><pre>
<del>template&lt;class T&gt;</del> void size(<del>T</del><ins>auto</ins>&amp;&amp;) = delete;
</pre></blockquote>
and does not include a declaration of <code>ranges::size</code><ins>, <code><i>decay-copy</i>(size(E))</code></ins>.</p></li>
</li>
<li><p>(1.3) &mdash; Otherwise, <code><i>make-unsigned-like</i>(ranges::end(E) - ranges::begin(E))</code> 
(<del>25.5.4 <a href="https://wg21.link/range.subrange">[range.subrange]</a></del><ins>25.2 <a href="https://wg21.link/ranges.syn">[ranges.syn]</a></ins>) if it is a valid 
expression and the types <code>I</code> and <code>S</code> of 
<code>ranges::begin(E)</code> and <code>ranges::end(E)</code> (respectively) model both
<code>sized_sentinel_for&lt;S, I&gt;</code> (24.3.4.8 <a href="https://wg21.link/iterator.concept.sizedsentinel">[iterator.concept.sizedsentinel]</a>) and 
<code>forward_iterator&lt;I&gt;</code>. However, <code>E</code> is evaluated only once.
</p></li>
<li><p>(1.4) &mdash; [&hellip;]</p></li>
</ol>
</blockquote>

</li>
</ol>





</body>
</html>
