<html>
<head>
<TITLE>
ISO/IEC JTC1/SC22/WG21
N4368
</TITLE>
</head>
<body>
<h1>
<img align=top src="/pics/iso44.gif" alt="ISO/">
<img align=top src="/pics/iec44.gif" alt="IEC">
JTC1/SC22/WG21
N4368
</h1>
<pre>
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&lt;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 &lt; flag_values.size(); 
      flag_number++ )
{
	method1( flag_number, flag_values[flag_number] );
}

//...

for ( special_flags_t::size_type flag_number = 0; 
      flag_number &lt; 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&lt;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&lt;bool>;

write 

using special_flags_t = std::bitset&lt;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&lt;size_t N> class bitset {
public:

typedef size_t size_type;
//,,,

bitset&lt;N>& operator&lt;&lt;=(size_type pos) noexcept;
bitset&lt;N>& operator>>=(size_type pos) noexcept;
//...
bitset&lt;N>& set(size_type pos, bool val = true);
//...
bitset&lt;N>& reset(size_type pos);
//...
bitset&lt;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&lt;N> operator&lt;&lt;(size_type pos) const noexcept;
bitset&lt;N> operator>>(size_type pos) const noexcept;


20.6.2 bitset members

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

//...

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

//...

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

//...

bitset&lt;N>& reset(size_type pos);

//...

bitset&lt;N>& flip(size_type pos);

//...

size_type count() const noexcept;

//...

constexpr size_type size() const noexcept;

//...

bool test(size_type pos) const;

//...

bitset&lt;N> operator&lt;&lt;(size_type pos) const noexcept;

//...

bitset&lt;N> operator>>(size_type pos) const noexcept;

//...

constexpr bool operator[](size_type pos) const;

//...

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

//...
</pre>
</body>
</html>
