<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Issue 907: Bitset's immutable element retrieval is inconsistently defined</title>
<meta property="og:title" content="Issue 907: Bitset's immutable element retrieval is inconsistently defined">
<meta property="og:description" content="C++ library issue. Status: C++11">
<meta property="og:url" content="https://cplusplus.github.io/LWG/issue907.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="907"><a href="lwg-defects.html#907">907</a>. Bitset's immutable element retrieval is inconsistently defined</h3>
<p><b>Section:</b> 22.9.2.3 <a href="https://wg21.link/bitset.members">[bitset.members]</a> <b>Status:</b> <a href="lwg-active.html#C++11">C++11</a>
 <b>Submitter:</b> Daniel Kr&uuml;gler <b>Opened:</b> 2008-09-26 <b>Last modified:</b> 2016-01-28</p>
<p><b>Priority: </b>Not Prioritized
</p>
<p><b>View all other</b> <a href="lwg-index.html#bitset.members">issues</a> in [bitset.members].</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>
The current standard 14882::2003(E) as well as the current draft
<a href="http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2008/n2723.pdf">N2723</a>
have in common a contradiction of the operational semantics of member function 
<code>test</code> 22.9.2.3 <a href="https://wg21.link/bitset.members">[bitset.members]</a> p.56-58 and the immutable
member <code>operator[]</code> overload 22.9.2.3 <a href="https://wg21.link/bitset.members">[bitset.members]</a> p.64-66 (all references
are defined in terms of
<a href="http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2008/n2723.pdf">N2723</a>):
</p>

<ol>
<li><pre>
bool test(size_t pos) const;
</pre>
<blockquote>
<p>
<i>Requires:</i> <code>pos</code> is valid
</p>
<p>
<i>Throws:</i> <code>out_of_range</code> if <code>pos</code> does not correspond
to a valid bit position.
</p>
<p>
<i>Returns:</i> <code>true</code> if the bit at position <code>pos</code> in <code>*this</code>
has the value one.
</p>
</blockquote>
</li>
<li><pre>
constexpr bool operator[](size_t pos) const;
</pre>
<blockquote>
<p>
<i>Requires:</i> <code>pos</code> shall be valid.
</p>
<p>
<i>Throws:</i> nothing.
</p>
<p>
<i>Returns:</i> <code>test(pos)</code>.
</p>
</blockquote>
</li>
</ol>

<p>
Three interpretations:
</p>

<ol style="list-style-type:upper-alpha">
<li>
The <code>operator[]</code> overload is indeed allowed to throw an exception
(via <code>test()</code>, if <code>pos</code> corresponds to an invalid bit position) which does
not leave the call frame. In this case this function cannot be a
<code>constexpr</code> function, because <code>test()</code> is not, due to
7.7 <a href="https://wg21.link/expr.const">[expr.const]</a>/2, last bullet.
</li>
<li>
The intend was not to throw an exception in <code>test</code> in case of an
invalid bit position. There is only little evidence for this interpretation.
</li>
<li>
The intend was that <code>operator[]</code> should not throw any exception,
but that <code>test</code> has the contract to do so, if the provided bit position
is invalid.
</li>
</ol>

<p>
The problem became worse, because issue <a href="lwg-defects.html#720" title="Omissions in constexpr usages (Status: CD1)">720</a><sup><a href="https://cplusplus.github.io/LWG/issue720" title="Latest snapshot">(i)</a></sup>
recently voted into WP argued that member <code>test</code> logically must be
a <code>constexpr</code> function, because it was used to define the semantics
of another <code>constexpr</code> function (the <code>operator[]</code> overload).
</p>

<p>
Three alternatives are proposed, corresponding to the three bullets
(A), (B), and (C), the author suggests to follow proposal (C).
</p>

<p><b>
Proposed alternatives:
</b></p>

<ol style="list-style-type:upper-alpha">
<li>
<p>
Remove the <code>constexpr</code> specifier in front of <code>operator[]</code> overload and
undo that of member <code>test</code> (assuming <a href="lwg-defects.html#720" title="Omissions in constexpr usages (Status: CD1)">720</a><sup><a href="https://cplusplus.github.io/LWG/issue720" title="Latest snapshot">(i)</a></sup> is accepted) in both the
class declaration 22.9.2 <a href="https://wg21.link/template.bitset">[template.bitset]</a>/1 and in the member description
before 22.9.2.3 <a href="https://wg21.link/bitset.members">[bitset.members]</a>/56 and before /64 to read:
</p>
<blockquote><pre>
<del>constexpr</del> bool test(size_t pos) const;
..
<del>constexpr</del> bool operator[](size_t pos) const;
</pre></blockquote>

<p>
Change the throws clause of p. 65 to read:
</p>

<blockquote><p>
<i>Throws:</i> <del>nothing</del>
<ins><code>out_of_range</code> if <code>pos</code> does not correspond to a valid bit
position</ins>.
</p></blockquote>
</li>
<li>
<p>
Replace the throws clause p. 57 to read:
</p>

<blockquote><p>
<i>Throws:</i> <del><code>out_of_range</code> if <code>pos</code> does not correspond to a valid bit
position</del> <ins>nothing</ins>.
</p></blockquote>
</li>
<li>
<p>
Undo the addition of the <code>constexpr</code> specifier to the <code>test</code> member
function in both class declaration 22.9.2 <a href="https://wg21.link/template.bitset">[template.bitset]</a>/1 and in the
member description before 22.9.2.3 <a href="https://wg21.link/bitset.members">[bitset.members]</a>/56, assuming that <a href="lwg-defects.html#720" title="Omissions in constexpr usages (Status: CD1)">720</a><sup><a href="https://cplusplus.github.io/LWG/issue720" title="Latest snapshot">(i)</a></sup>
was applied.
</p>

<blockquote><pre>
<del>constexpr</del> bool test(size_t pos) const;
</pre></blockquote>

<p>
Change the returns clause p. 66 to read:
</p>

<blockquote><p>
<i>Returns:</i> <del><code>test(pos)</code></del> <ins><code>true</code> if the bit at position <code>pos</code> in <code>*this</code>
has the value one, otherwise <code>false</code></ins>.
</p></blockquote>
</li>
</ol>

<p><i>[
Post Summit:
]</i></p>


<blockquote>
<p>
Lawrence: proposed resolutions A, B, C are mutually exclusive.
</p>
<p>
Recommend Review with option C.
</p>
</blockquote>

<p><i>[
Batavia (2009-05):
]</i></p>

<blockquote><p>
We agree with the proposed resolution.
Move to Tentatively Ready.
</p></blockquote>


<p id="res-907"><b>Proposed resolution:</b></p>

<ol style="list-style-type:upper-alpha">
<li>[&hellip;]</li>
<li>[&hellip;]</li>
<li>
<p>
Undo the addition of the <code>constexpr</code> specifier to the <code>test</code> member
function in both class declaration 22.9.2 <a href="https://wg21.link/template.bitset">[template.bitset]</a> p.1 and in the
member description before 22.9.2.3 <a href="https://wg21.link/bitset.members">[bitset.members]</a> p.56, assuming that <a href="lwg-defects.html#720" title="Omissions in constexpr usages (Status: CD1)">720</a><sup><a href="https://cplusplus.github.io/LWG/issue720" title="Latest snapshot">(i)</a></sup>
was applied.
</p>

<blockquote><pre>
<del>constexpr</del> bool test(size_t pos) const;
</pre></blockquote>

<p>
Change the returns clause p. 66 to read:
</p>

<blockquote><p>
<i>Returns:</i> <del><code>test(pos)</code></del> <ins><code>true</code> if the bit at position <code>pos</code> in <code>*this</code>
has the value one, otherwise <code>false</code></ins>.
</p></blockquote>
</li>
</ol>






</body>
</html>
