在前几天阅读了cppreference上关于bitset
的有关知识,我注意到在cppreference/bitset中, bitset 的数据结构并没有用于保存位模式副本的数据成员。而在其它的一些资料中显式, bitset 是继承自_Base_bitset
的,并在 _Base_bitset 中设计一个unsigned long
数组用于保存位模式副本。
那么cppreference是否省略了这种继承关系?
答案是没有。std::bitset
可以在 C++ 标准的22.9 Utilities.bitset中找到。它没有提及 _Base_bitset 或其他细节,因为这些都留给了库实现者来实现。此外,并非所有实现都使用_Base_bitset, MSVC 就没有。
概要std::bitset
如下。其余的是不相关的实现细节,可能会改变,并且不应该影响格式良好的代码。换句话说就是,库实现者可能会修改下面概要中的部分实现细节,但是不应该对已有的接口进行修改。
namespace std {
template<size_t N> class bitset {
public:
// bit reference
class reference {
friend class bitset;
reference() noexcept;
public:
reference(const reference&) = default;
~reference();
reference& operator=(bool x) noexcept; // for b[i] = x;
reference& operator=(const reference&) noexcept; // for b[i] = b[j];
bool operator~() const noexcept; // flips the bit
operator bool() const noexcept; // for x = b[i];
reference& flip() noexcept; // for b[i].flip();
};
// [bitset.cons], constructors
constexpr bitset() noexcept;
constexpr bitset(unsigned long long val) noexcept;
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,
charT zero = charT('0'),
charT one = charT('1'));
template<class charT>
explicit bitset(
const charT* str,
typename basic_string<charT>::size_type n = basic_string<charT>::npos,
charT zero = charT('0'),
charT one = charT('1'));
// [bitset.members], bitset operations
bitset& operator&=(const bitset& rhs) noexcept;
bitset& operator|=(const bitset& rhs) noexcept;
bitset& operator^=(const bitset& rhs) noexcept;
bitset& operator<<=(size_t pos) noexcept;
bitset& operator>>=(size_t pos) noexcept;
bitset& set() noexcept;
bitset& set(size_t pos, bool val = true);
bitset& reset() noexcept;
bitset& reset(size_t pos);
bitset operator~() const noexcept;
bitset& flip() noexcept;
bitset& flip(size_t pos);
// element access
constexpr 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 = char,
class traits = char_traits<charT>,
class Allocator = allocator<charT>>
basic_string<charT, traits, Allocator>
to_string(charT zero = charT('0'), charT one = charT('1')) const;
size_t count() const noexcept;
constexpr size_t size() const noexcept;
bool operator==(const bitset& rhs) const noexcept;
bool test(size_t pos) const;
bool all() const noexcept;
bool any() const noexcept;
bool none() const noexcept;
bitset operator<<(size_t pos) const noexcept;
bitset operator>>(size_t pos) const noexcept;
};
// [bitset.hash], hash support
template<class T> struct hash;
template<size_t N> struct hash<bitset<N>>;
}