<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Issue 1197: Can unordered containers have bucket_count() == 0?</title>
<meta property="og:title" content="Issue 1197: Can unordered containers have bucket_count() == 0?">
<meta property="og:description" content="C++ library issue. Status: C++11">
<meta property="og:url" content="https://cplusplus.github.io/LWG/issue1197.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="1197"><a href="lwg-defects.html#1197">1197</a>. Can unordered containers have <code>bucket_count() == 0</code>?</h3>
<p><b>Section:</b> 23.2.8 <a href="https://wg21.link/unord.req">[unord.req]</a> <b>Status:</b> <a href="lwg-active.html#C++11">C++11</a>
 <b>Submitter:</b> Howard Hinnant <b>Opened:</b> 2009-08-24 <b>Last modified:</b> 2016-01-28</p>
<p><b>Priority: </b>Not Prioritized
</p>
<p><b>View other</b> <a href="lwg-index-open.html#unord.req">active issues</a> in [unord.req].</p>
<p><b>View all other</b> <a href="lwg-index.html#unord.req">issues</a> in [unord.req].</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>
Table 97 "Unordered associative container requirements" in
23.2.8 <a href="https://wg21.link/unord.req">[unord.req]</a> says:
</p>

<blockquote>
<table border="1">
<caption>Table 97 &mdash; Unordered associative container requirements
(in addition to container)</caption>

<tr>
<th>Expression</th>
<th>Return type</th>
<th>Assertion/note pre-/post-condition</th>
<th>Complexity</th>
</tr>

<tr>
<td><code>b.bucket(k)</code></td>
<td><code>size_type</code></td>
<td>Returns the index of the bucket in which elements with keys 
equivalent to <code>k</code> would be found, 
if any such element existed. 
Post: the return value shall be 
in the range <code>[0, 
b.bucket_count())</code>.</td>
<td>Constant</td>
</tr>

</table>
</blockquote>

<p>
What should <code>b.bucket(k)</code> return if <code>b.bucket_count() == 0</code>?
</p>

<p>
I believe allowing <code>b.bucket_count() == 0</code> is important.  It is a
very reasonable post-condition of the default constructor, or of a moved-from
container.
</p>

<p>
I can think of several reasonable results from <code>b.bucket(k)</code> when
<code>b.bucket_count() == 0</code>:
</p>

<ol>
<li>
Return 0.
</li>
<li>
Return <code>numeric_limits&lt;size_type&gt;::max()</code>.
</li>
<li>
Throw a <code>domain_error</code>.
</li>
<li>
Requires: <code>b.bucket_count() != 0</code>.
</li>
</ol>

<p><i>[
2009-08-26 Daniel adds:
]</i></p>


<blockquote>
<p>
A forth choice would be to add the pre-condition "<code>b.bucket_count() != 0</code>"
and thus imply undefined behavior if this is violated.
</p>

<p><i>[
Howard:  I like this option too, added to the list.
]</i></p>


<p>
Further on here my own favorite solution (rationale see below):
</p>

<p><b>Suggested resolution:</b></p>

<p>
[Rationale: I suggest to follow choice (1). The main reason is
that all associative container functions which take a key argument,
are basically free of pre-conditions and non-disrupting, therefore
excluding choices (3) and (4). Option (2) seems a bit unexpected
to me. It would be more natural, if several similar functions
would exist which would also justify the existence of a symbolic
constant like npos for this situation. The value 0 is both simple
and consistent, it has exactly the same role as a past-the-end
iterator value. A typical use-case is:
</p>

<blockquote><pre>
size_type pos = m.bucket(key);
if (pos != m.bucket_count()) {
 ...
} else {
 ...
}
</pre></blockquote>

<p>&mdash; end Rationale]</p>

<p>
- Change Table 97 in 23.2.8 <a href="https://wg21.link/unord.req">[unord.req]</a> as follows (Row b.bucket(k), Column "Assertion/..."):
</p>

<blockquote>
<table border="1">
<caption>Table 97 &mdash; Unordered associative container requirements
(in addition to container)</caption>

<tr>
<th>Expression</th>
<th>Return type</th>
<th>Assertion/note pre-/post-condition</th>
<th>Complexity</th>
</tr>

<tr>
<td><code>b.bucket(k)</code></td>
<td><code>size_type</code></td>
<td>Returns the index of the bucket in which elements with keys 
equivalent to <code>k</code> would be found, 
if any such element existed. 
Post: <ins>if b.bucket_count() != 0, </ins>the return value shall be 
in the range <code>[0, 
b.bucket_count())</code><ins>, otherwise 0</ins>.</td>
<td>Constant</td>
</tr>

</table>
</blockquote>

</blockquote>

<p><i>[
2010-01-25 Choice 4 put into proposed resolution section.
]</i></p>


<p><i>[
2010-01-31 Moved to Tentatively Ready after 5 positive votes on c++std-lib.
]</i></p>



<p id="res-1197"><b>Proposed resolution:</b></p>
<p>
Change Table 97 in 23.2.8 <a href="https://wg21.link/unord.req">[unord.req]</a> as follows (Row b.bucket(k), Column
"Assertion/..."):
</p>

<blockquote>
<table border="1">
<caption>Table 97 &mdash; Unordered associative container requirements
(in addition to container)</caption>

<tr>
<th>Expression</th>
<th>Return type</th>
<th>Assertion/note pre-/post-condition</th>
<th>Complexity</th>
</tr>

<tr>
<td><code>b.bucket(k)</code></td>
<td><code>size_type</code></td>
<td><ins>Pre:  <code>b.bucket_count() &gt; 0</code></ins> Returns the index of the
bucket in which elements with keys equivalent to <code>k</code> would be found, if
any such element existed. Post: the return value shall be in the range <code>[0,
b.bucket_count())</code>.</td>
<td>Constant</td>
</tr>

</table>
</blockquote>






</body>
</html>
