<h2 id="ssizeshouldbenamedcount">ssize Should be Named count</h2>

<p>Document number: P1764r0 <br />
Date: 2019-06-17 <br />
Audience: LEWG <br />
Reply-to: Tony Van Eerd. count at forecode.com</p>

<hr />

<blockquote>
  <p>I've been through the papers, found some stuff with bad names <br />
  It felt bad to be causing dev pain <br />
  In the standard you need stuff with good names <br />
  Cause there ain't no one for to help you explain <br />
  La la, la la-la la  </p>
  
  <p>After two days in the standard tome <br />
  My brain began to turn dead <br />
  After three days with the standard combed <br />
  I was looking at a lot of dread <br />
  And the story it told of those names in code <br />
  Made me sad to think of our devs  </p>
  
  <p>You see I've been through the papers, found some stuff with bad names <br />
  It felt bad to be causing dev pain <br />
  In the standard you need stuff with good names <br />
  Cause there ain't no one for to help you explain <br />
  La la, la la-la la <br />
  .... <br />
  (ie <em>A Horse with No Name</em> by America)</p>
</blockquote>

<hr />

<h2 id="summary">Summary</h2>

<p>To resolve (or at least temporarily quell) the signed/unsigned debate, P1227 (approved in Kona) proposes adding a member function <code>ssize()</code> to <code>span</code>,
which is just like <code>size()</code>, but which returns a signed value.  It also proposed adding <code>std::ssize(container)</code> for all standard containers,
and <code>ssize()</code> member functions for all containers, not just <code>span</code>.</p>

<p>Bjarne's P1491 (and P1428) however notes:</p>

<blockquote>
  <p>[p1227R1] proposes to change <code>size()</code> in the ranges TS and for <code>span</code> to <code>unsigned</code> (making them bug compatible with the STL)
  and adding <code>ssize()</code> to all containers and range accessors</p>
  
  <ul>
  <li>embeds a type in a function name (and it makes me think of Parseltongue :-) )</li>
  
  <li>leaves the wrong solution (IMO) with the better, more established, and simpler name</li>
  </ul>
</blockquote>

<p>Thus this proposal - Use the name <code>count</code> instead of <code>ssize</code></p>

<ul>
<li>does not embed type info</li>

<li>is a better name than <code>ssize</code></li>

<li>is actually a better name than <code>size</code> (which is confused with <code>sizeof</code>)</li>

<li>will eventually (20years?) become the established, simple, more intent-ful name</li>
</ul>

<h2 id="currentusesofcount">Current uses of <code>count()</code></h2>

<p>Functions named <code>count</code> can already be found in the standard.</p>

<h3 id="consistentcompatibleexistingusageofcount">Consistent/Compatible existing usage of "count"</h3>

<ul>
<li><code>AssociativeContainer::count(key)</code> returns the number of items with <code>key</code>.  This is consistent with a function <code>AssociativeContainer::count()</code> returning the number of <em>all</em> the items.</li>
</ul>

<p>Similarly, these Algorithms:</p>

<ul>
<li>std::[range]::count(first, last, value [, projection])</li>

<li>std::[range]::count_if(first,last, predicate [, projection])</li>

<li>std::range::count(range, value [, projection])</li>

<li>std::range::count_if(range, predicate [, projection])</li>
</ul>

<p>are consistent with a std::count(container/range) that would return the total number of elements of the container/range</p>

<p>(P.S. the above algorithms return signed values)</p>

<ul>
<li><p>Ranges also has <code>counted_iterator::count()</code> returning the "length" of the iterator (ie number of iterations to get to end). (also a signed value)</p></li>

<li><p>chrono: duration.count() - number of ticks</p></li>
</ul>

<p>All the above are consistent with using the name <code>count</code> for the number of items in a range or container.</p>

<h3 id="whywecanthavenicethings">Why We can't have nice things</h3>

<p><code>bitset&lt;N&gt;::count</code> returns the number of <em>on</em> bits.  Not the size (ie not the number of total bits, both on and off).</p>

<p>We could deprecate <code>count()</code> on bitset, replace it with what it really does - <code>popcount</code> (ie "population count" if you ever wondered what popcount meant). Or <code>count_on</code>, etc.</p>

<p>And then wait 10-15 years, then add <code>int count() // returns number of elements</code> to bitset.</p>

<h2 id="corollary">Corollary</h2>

<p>Introduce <code>std::count_t</code> as the type returned by <code>count()</code>.</p>

<p>Currently, no one knows what type to use in a simple for loop.  <code>int</code>? <code>size_t</code>? <code>ptrdiff_t</code>? <code>int64_t</code>?  None of them suggest the intent of counting the cardinality of a container.  <code>int</code> was suppose to be "the native size" of the architecture, but the move from 32bit to 64bit processors somehow left <code>int</code> behind, unfortunately. We embarassingly have no answer for how to count.</p>