Title
std::bitset and long long
Status
cd1
Section
[template.bitset]
Submitter
Martin Sebor

Created on 2007-06-22.00:00:00 last changed 171 months ago

Messages

Date: 2010-10-21.18:28:33

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 [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 [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 [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.

Date: 2007-06-22.00:00:00

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.

History
Date User Action Args
2010-10-21 18:28:33adminsetmessages: + msg3459
2007-06-22 00:00:00admincreate