This page is a snapshot from the LWG issues list, see the Library Active Issues List for more information and the meaning of CD1 status.

694. std::bitset and long long

Section: 22.9.2 [template.bitset] Status: CD1 Submitter: Martin Sebor Opened: 2007-06-22 Last modified: 2016-01-28

Priority: Not Prioritized

View all other issues in [template.bitset].

View all issues with CD1 status.

Discussion:

Objects of the bitset class template specializations can be constructed from and explicitly converted to values of the widest C++ integer type, unsigned long. With the introduction of long long into the language the template should be enhanced to make it possible to interoperate with values of this type as well, or perhaps uintmax_t. See c++std-lib-18274 for a brief discussion in support of this change.

Proposed resolution:

For simplicity, instead of adding overloads for unsigned long long and dealing with possible ambiguities in the spec, replace the bitset ctor that takes an unsigned long argument with one taking unsigned long long 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.)

// [bitset.cons] constructors:
bitset();
bitset(unsigned long long val);
template<class charT, class traits, class Allocator>
explicit bitset(
                const basic_string<charT,traits,Allocator>& str,
                typename basic_string<charT,traits,Allocator>::size_type pos = 0,
                typename basic_string<charT,traits,Allocator>::size_type n =
                    basic_string<charT,traits,Allocator>::npos);

Make a corresponding change in 22.9.2.2 [bitset.cons], p2:

bitset(unsigned long long val);

Effects: Constructs an object of class bitset<N>, initializing the first M bit positions to the corresponding bit values in val. M is the smaller of N and the number of bits in the value representation (section [basic.types]) of unsigned long long. If M < N is true, the remaining bit positions are initialized to zero.

Additionally, introduce a new member function to_ullong() to make it possible to convert bitset to values of the new type. Add the following declaration to the definition of the template, immediate after the declaration of to_ulong() in 22.9.2 [template.bitset], p1, as shown below:

// element access:
bool operator[](size_t pos) const; // for b[i];
reference operator[](size_t pos); // for b[i];
unsigned long to_ulong() const;
unsigned long long to_ullong() const;
template <class charT, class traits, class Allocator>
basic_string<charT, traits, Allocator> to_string() const;

And add a description of the new member function to 22.9.2.3 [bitset.members], below the description of the existing to_ulong() (if possible), with the following text:

unsigned long long to_ullong() const;

Throws: overflow_error if the integral value x corresponding to the bits in *this cannot be represented as type unsigned long long.

Returns: x.