JTC1/SC22/WG21
N4443
Introducing alias size_type for type size_t in class std::bitset (Rev. 1 ).
Document number: N4443
Revises: N4368
Project: Programming Language C++, Library Evolution Working Group
Date: 2015-04-09
Date of the Revision: 2015-02-09
Reply-To: Vladimir Grigoriev (vlad.moscow@mail.ru)
Revision history.
In the preceding version of the document there was suggested
to include a typedef definition of size_type in class
std::bitset.
This revision of the document suggests to include also
a typedef definition value_type in the class.
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 typedef definitions size_type for size_t
and value_type for bool in the class definition.
Also all member functions that uses types size_t
and bool as the type of a parameter
or of the return value should be
changed such a way that instead of size_t and
bool there would be used the typedef names
size_type and value_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;
typedef bool value_type;
//,,,
bitset<N>& operator<<=(size_type pos) noexcept;
bitset<N>& operator>>=(size_type pos) noexcept;
//...
bitset<N>& set(size_type pos, value_type val = true);
//...
bitset<N>& reset(size_type pos);
//...
bitset<N>& flip(size_type pos);
//...
constexpr value_type 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, value_type 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 value_type operator[](size_type pos) const;
//...
bitset<N>::reference operator[](size_type pos);
//...