ISO/ IEC JTC1/SC22/WG21 N4368

Introducing alias size_type for type size_t in class std::bitset.

Document number: N4368

Project: Programming Language C++, Library Evolution Working Group

Date: 2015-02-03
Reply-To: Vladimir Grigoriev (vlad.moscow@mail.ru)

Preface. 

It is difficult to write generic and portable code if standard classes 
do not provide common forms of address for their properties.

Let's consider a simple example. Let's assume that you have a project 
where there is a set of flags that are used in some methods. 

For example

using special_flags_t = std::vector<bool>;

void method1( special_flags_t::size_type flag_number, bool flag_value )
{
	// some processing using the flag
}

//...

void methodn( special_flags_t::size_type flag_number, bool flag_value )
{
	// some processing using the flag
}

//...

special_flags_t flag_values;

//...

for ( special_flags_t::size_type flag_number = 0; 
      flag_number < flag_values.size(); 
      flag_number++ )
{
	method1( flag_number, flag_values[flag_number] );
}

//...

for ( special_flags_t::size_type flag_number = 0; 
      flag_number < flag_values.size(); 
      flag_number++ )
{
	methodn( flag_number, flag_values[flag_number] );
}

And let's assume that after a time you concluded that it 
would be better to replace std::vector<bool> with std::bitset 
because the set of flags has a small fixed size. 
It would be great if it will enogh to substitute only 
for the alias declaration. 

However if you make the substitution and instead of 

using special_flags_t = std::vector<bool>;

write 

using special_flags_t = std::bitset<N>;

(where N is some predefined constant), the project will not compile 
and the compiler will issue numerous errors. 

The problem is that the standard class std::bitset does not provide
the common form of address size_type for its property size. 
 
Changes to the C++ Standard.

Include a typedef definition size_type for size_t 
in the class definition. 
Also all member functions that uses type size_t as
the type of a parameter or of the return value should be
changed such a way that instead of size_t there would
be used the typedef name size_type as it is shown below.

20.6 Class template bitset
//...
namespace std {
template<size_t N> class bitset {
public:

typedef size_t size_type;
//,,,

bitset<N>& operator<<=(size_type pos) noexcept;
bitset<N>& operator>>=(size_type pos) noexcept;
//...
bitset<N>& set(size_type pos, bool val = true);
//...
bitset<N>& reset(size_type pos);
//...
bitset<N>& flip(size_type pos);
//...

constexpr bool operator[](size_type pos) const; // for b[i];
reference operator[](size_type pos); // for b[i];

//...

size_type count() const noexcept;
constexpr size_type size() const noexcept;

//...

bool test(size_type pos) const;

//...

bitset<N> operator<<(size_type pos) const noexcept;
bitset<N> operator>>(size_type pos) const noexcept;


20.6.2 bitset members

//...
bitset<N>& operator<<=(size_type pos) noexcept;

//...

bitset<N>& operator>>=(size_type pos) noexcept;

//...

bitset<N>& set(size_type pos, bool val = true);

//...

bitset<N>& reset(size_type pos);

//...

bitset<N>& flip(size_type pos);

//...

size_type count() const noexcept;

//...

constexpr size_type size() const noexcept;

//...

bool test(size_type pos) const;

//...

bitset<N> operator<<(size_type pos) const noexcept;

//...

bitset<N> operator>>(size_type pos) const noexcept;

//...

constexpr bool operator[](size_type pos) const;

//...

bitset<N>::reference operator[](size_type pos);

//...