<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Issue 694: std::bitset and long long</title>
<meta property="og:title" content="Issue 694: std::bitset and long long">
<meta property="og:description" content="C++ library issue. Status: CD1">
<meta property="og:url" content="https://cplusplus.github.io/LWG/issue694.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#CD1">CD1</a> status.</em></p>
<h3 id="694"><a href="lwg-defects.html#694">694</a>. <code>std::bitset</code> and <code>long long</code></h3>
<p><b>Section:</b> 22.9.2 <a href="https://wg21.link/template.bitset">[template.bitset]</a> <b>Status:</b> <a href="lwg-active.html#CD1">CD1</a>
 <b>Submitter:</b> Martin Sebor <b>Opened:</b> 2007-06-22 <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#template.bitset">active issues</a> in [template.bitset].</p>
<p><b>View all other</b> <a href="lwg-index.html#template.bitset">issues</a> in [template.bitset].</p>
<p><b>View all issues with</b> <a href="lwg-status.html#CD1">CD1</a> status.</p>
<p><b>Discussion:</b></p>
<p>
Objects of the  <code>bitset</code> class template specializations can
be constructed from  and explicitly converted to values  of the widest
C++ integer  type, <code>unsigned long</code>.   With the introduction
of  <code>long long</code> into  the language  the template  should be
enhanced to make it possible  to interoperate with values of this type
as well, or  perhaps <code>uintmax_t</code>.  See c++std-lib-18274 for
a brief discussion in support of this change.
</p>


<p id="res-694"><b>Proposed resolution:</b></p>
<p>
For simplicity,  instead of  adding overloads for  <code>unsigned long
long</code> and dealing with possible ambiguities in the spec, replace
the <code>bitset</code> ctor  that takes an <code>unsigned long</code>
argument  with  one  taking  <code>unsigned long  long</code>  in  the
definition  of the  template as  shown below.   (The  standard permits
implementations  to add  overloads on  other integer  types  or employ
template tricks to  achieve the same effect provided  they don't cause
ambiguities or changes in behavior.)
</p>
<blockquote>
<pre>
// [bitset.cons] constructors:
bitset();
bitset(unsigned <ins>long</ins> long val);
template&lt;class charT, class traits, class Allocator&gt;
explicit bitset(
                const basic_string&lt;charT,traits,Allocator&gt;&amp; str,
                typename basic_string&lt;charT,traits,Allocator&gt;::size_type pos = 0,
                typename basic_string&lt;charT,traits,Allocator&gt;::size_type n =
                    basic_string&lt;charT,traits,Allocator&gt;::npos);
</pre>
</blockquote>
<p>
Make a corresponding change in 22.9.2.2 <a href="https://wg21.link/bitset.cons">[bitset.cons]</a>, p2:
</p>
<blockquote>
<p>
<code>bitset(unsigned <ins>long</ins> long val);</code>
</p>
<blockquote><p>
<i>Effects</i>:  Constructs   an  object  of   class  bitset&lt;N&gt;,
initializing  the  first <code><i>M</i></code>  bit  positions to  the
corresponding      bit     values      in     <code><i>val</i></code>.
<code><i>M</i></code> is the  smaller of <code><i>N</i></code> and the
number of bits in  the value representation (section [basic.types]) of
<code>unsigned  <ins> long</ins> long</code>.   If  <code><i>M</i> &lt;
<i>N</i></code>  <ins>is  <code>true</code></ins>,  the remaining  bit
positions are initialized to zero.
</p></blockquote>
</blockquote>

<p>
Additionally, introduce a new member function <code>to_ullong()</code>
to make  it possible to  convert <code>bitset</code> to values  of the
new  type. Add  the following  declaration  to the  definition of  the
template, immediate  after the declaration  of <code>to_ulong()</code>
in 22.9.2 <a href="https://wg21.link/template.bitset">[template.bitset]</a>, p1, as shown below:
</p>
<blockquote>
<pre>
// element access:
bool operator[](size_t pos) const; // for b[i];
reference operator[](size_t pos); // for b[i];
unsigned long to_ulong() const;
<ins>unsigned long long to_ullong() const;</ins>
template &lt;class charT, class traits, class Allocator&gt;
basic_string&lt;charT, traits, Allocator&gt; to_string() const;
</pre>
</blockquote>
<p>
And add a description of  the new member function to 22.9.2.3 <a href="https://wg21.link/bitset.members">[bitset.members]</a>,
below  the  description of  the  existing <code>to_ulong()</code>  (if
possible), with the following text:
</p>
<blockquote>
<p>
<code>unsigned long long to_ullong() const;</code>
</p>
<blockquote><p>
<i>Throws</i>:  <code>overflow_error</code>   if  the  integral  value
<code><i>x</i></code> corresponding to  the bits in <code>*this</code>
cannot be represented as type <code>unsigned long long</code>.
</p></blockquote>
<blockquote><p>
<i>Returns:</i> <code><i>x</i></code>.
</p></blockquote>
</blockquote>





</body>
</html>
