Go to the documentation of this file. 1// Debugging bitset implementation -*- C++ -*-
3// Copyright (C) 2003-2025 Free Software Foundation, Inc.
5// This file is part of the GNU ISO C++ Library. This library is free
6// software;
you can redistribute it and/or modify it under the
7// terms of the GNU General Public License as published by the
8// Free Software Foundation;
either version 3, or (at your option)
11// This library is distributed in the hope that it will be useful,
12// but WITHOUT ANY WARRANTY;
without even the implied warranty of
13// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14// GNU General Public License for more details.
16// Under Section 7 of GPL version 3, you are granted additional
17// permissions described in the GCC Runtime Library Exception, version
18// 3.
1, as published by the Free Software Foundation.
20// You should have received a copy of the GNU General Public License and
21// a copy of the GCC Runtime Library Exception along with this program;
22// see the files COPYING3 and COPYING.RUNTIME respectively. If not, see
23// <http://www.gnu.org/licenses/>.
26 * This file is a GNU debug extension to the Standard C++ Library.
29#ifndef _GLIBCXX_DEBUG_BITSET
30#define _GLIBCXX_DEBUG_BITSET
33#pragma GCC system_header
37#include <debug/safe_sequence.h>
38#include <debug/safe_iterator.h>
40namespace std _GLIBCXX_VISIBILITY(default)
44 /// Class std::bitset with additional safety/checking/debug instrumentation.
47 : public _GLIBCXX_STD_C::bitset<_Nb>
48#if __cplusplus < 201103L
49 , public __gnu_debug::_Safe_sequence_base
52 typedef _GLIBCXX_STD_C::bitset<_Nb> _Base;
55 // In C++11 we rely on normal reference type to preserve the property
56 // of bitset to be use as a literal.
57 // TODO: Find another solution.
58#if __cplusplus >= 201103L
59 typedef typename _Base::reference reference;
63 : private _Base::reference
64 , public __gnu_debug::_Safe_iterator_base
66 typedef typename _Base::reference _Base_ref;
71 reference(const _Base_ref& __base, bitset* __seq) _GLIBCXX_NOEXCEPT
73 , _Safe_iterator_base(__seq, false)
77 reference(const reference& __x) _GLIBCXX_NOEXCEPT
79 , _Safe_iterator_base(__x, false)
83 operator=(bool __x) _GLIBCXX_NOEXCEPT
85 _GLIBCXX_DEBUG_VERIFY(!this->_M_singular(),
86 _M_message(__gnu_debug::__msg_bad_bitset_write)
88 *static_cast<_Base_ref*>(this) = __x;
93 operator=(const reference& __x) _GLIBCXX_NOEXCEPT
95 _GLIBCXX_DEBUG_VERIFY(!__x._M_singular(),
96 _M_message(__gnu_debug::__msg_bad_bitset_read)
98 _GLIBCXX_DEBUG_VERIFY(!this->_M_singular(),
99 _M_message(__gnu_debug::__msg_bad_bitset_write)
100 ._M_iterator(*this));
101 *static_cast<_Base_ref*>(this) = __x;
106 operator~() const _GLIBCXX_NOEXCEPT
108 _GLIBCXX_DEBUG_VERIFY(!this->_M_singular(),
109 _M_message(__gnu_debug::__msg_bad_bitset_read)
110 ._M_iterator(*this));
111 return ~(*static_cast<const _Base_ref*>(this));
114 operator bool() const _GLIBCXX_NOEXCEPT
116 _GLIBCXX_DEBUG_VERIFY(!this->_M_singular(),
117 _M_message(__gnu_debug::__msg_bad_bitset_read)
118 ._M_iterator(*this));
119 return *static_cast<const _Base_ref*>(this);
123 flip() _GLIBCXX_NOEXCEPT
125 _GLIBCXX_DEBUG_VERIFY(!this->_M_singular(),
126 _M_message(__gnu_debug::__msg_bad_bitset_flip)
127 ._M_iterator(*this));
134 // 23.
3.
5.
1 constructors:
135 _GLIBCXX_CONSTEXPR bitset() _GLIBCXX_NOEXCEPT
138#if __cplusplus >= 201103L
139 constexpr bitset(unsigned long long __val) noexcept
141 bitset(unsigned long __val)
145 template<typename _CharT, typename _Traits, typename _Alloc>
148 bitset(const std::basic_string<_CharT, _Traits, _Alloc>& __str,
149 typename std::basic_string<_CharT, _Traits, _Alloc>::size_type
151 typename std::basic_string<_CharT, _Traits, _Alloc>::size_type
152 __n = (std::basic_string<_CharT, _Traits, _Alloc>::npos))
153 : _Base(__str, __pos, __n) { }
155 // _GLIBCXX_RESOLVE_LIB_DEFECTS
156 // 396.
what are characters zero and one.
157 template<class _CharT, class _Traits, class _Alloc>
159 bitset(const std::basic_string<_CharT, _Traits, _Alloc>& __str,
160 typename std::basic_string<_CharT, _Traits, _Alloc>::size_type
162 typename std::basic_string<_CharT, _Traits, _Alloc>::size_type
164 _CharT __zero, _CharT __one = _CharT('1'))
165 : _Base(__str, __pos, __n, __zero, __one) { }
167#ifdef __cpp_lib_bitset // ... from string_view
168 template<class _CharT, class _Traits>
170 bitset(std::basic_string_view<_CharT, _Traits> __s,
171 std::basic_string_view<_CharT, _Traits>::size_type __position = 0,
172 std::basic_string_view<_CharT, _Traits>::size_type __n =
173 std::basic_string_view<_CharT, _Traits>::npos,
174 _CharT __zero = _CharT('0'), _CharT __one = _CharT('1'))
175 : _Base(__s, __position, __n, __zero, __one) { }
179 bitset(const _Base& __x) : _Base(__x) { }
181#if __cplusplus >= 201103L
182 // _GLIBCXX_RESOLVE_LIB_DEFECTS
183 // 4294.
bitset(const CharT*) constructor needs to be constrained
184 template<typename _CharT,
185 typename = _Require<is_trivially_copyable<_CharT>,
186 is_standard_layout<_CharT>,
187 is_trivially_default_constructible<_CharT>,
188 __not_<is_array<_CharT>>>>
191 bitset(const _CharT* __str,
192 typename std::basic_string<_CharT>::size_type __n
193 = std::basic_string<_CharT>::npos,
194 _CharT __zero = _CharT('0'), _CharT __one = _CharT('1'))
195 : _Base(__str, __n, __zero, __one) { }
198 // 23.
3.
5.
2 bitset operations:
201 operator&=(const bitset<_Nb>& __rhs) _GLIBCXX_NOEXCEPT
209 operator|=(const bitset<_Nb>& __rhs) _GLIBCXX_NOEXCEPT
217 operator^=(const bitset<_Nb>& __rhs) _GLIBCXX_NOEXCEPT
225 operator<<=(size_t __pos) _GLIBCXX_NOEXCEPT
233 operator>>=(size_t __pos) _GLIBCXX_NOEXCEPT
241 set() _GLIBCXX_NOEXCEPT
247 // _GLIBCXX_RESOLVE_LIB_DEFECTS
248 // 186.
bitset::set() second parameter should be bool
251 set(size_t __pos, bool __val = true)
253 _Base::set(__pos, __val);
259 reset() _GLIBCXX_NOEXCEPT
275 operator~() const _GLIBCXX_NOEXCEPT
276 {
return bitset(~_M_base()); }
280 flip() _GLIBCXX_NOEXCEPT
295 // _GLIBCXX_RESOLVE_LIB_DEFECTS
296 // 11.
Bitset minor problems
299 operator[]
(size_t __pos)
301 __glibcxx_check_subscript(__pos);
302#if __cplusplus >= 201103L
303 return _M_base()[
__pos];
305 return reference(_M_base()[
__pos]
, this);
309 // _GLIBCXX_RESOLVE_LIB_DEFECTS
310 // 11.
Bitset minor problems
311 _GLIBCXX_CONSTEXPR bool
312 operator[]
(size_t __pos) const
314#if __cplusplus < 201103L
315 // TODO: Check in debug-mode too.
316 __glibcxx_check_subscript(__pos);
318 return _Base::operator[]
(__pos);
321 using _Base::to_ulong;
322#if __cplusplus >= 201103L
323 using _Base::to_ullong;
326 template <typename _CharT, typename _Traits, typename _Alloc>
328 std::basic_string<_CharT, _Traits, _Alloc>
330 {
return _M_base().template to_string<_CharT, _Traits, _Alloc>(); }
332 // _GLIBCXX_RESOLVE_LIB_DEFECTS
333 // 396.
what are characters zero and one.
334 template<class _CharT, class _Traits, class _Alloc>
336 std::basic_string<_CharT, _Traits, _Alloc>
337 to_string(_CharT __zero, _CharT __one = _CharT('1')) const
339 return _M_base().template
340 to_string<_CharT, _Traits, _Alloc>(__zero, __one);
343 // _GLIBCXX_RESOLVE_LIB_DEFECTS
344 // 434.
bitset::to_string() hard to use.
345 template<typename _CharT, typename _Traits>
347 std::basic_string<_CharT, _Traits, std::allocator<_CharT> >
349 {
return to_string<_CharT, _Traits, std::allocator<_CharT> >(); }
351 // _GLIBCXX_RESOLVE_LIB_DEFECTS
352 // 853.
to_string needs updating with zero and one.
353 template<class _CharT, class _Traits>
355 std::basic_string<_CharT, _Traits, std::allocator<_CharT> >
356 to_string(_CharT __zero, _CharT __one = _CharT('1')) const
357 {
return to_string<_CharT, _Traits,
358 std::allocator<_CharT> >(__zero, __one); }
360 template<typename _CharT>
362 std::basic_string<_CharT, std::char_traits<_CharT>,
363 std::allocator<_CharT> >
366 return to_string<_CharT, std::char_traits<_CharT>,
367 std::allocator<_CharT> >();
370 template<class _CharT>
372 std::basic_string<_CharT, std::char_traits<_CharT>,
373 std::allocator<_CharT> >
374 to_string(_CharT __zero, _CharT __one = _CharT('1')) const
376 return to_string<_CharT, std::char_traits<_CharT>,
377 std::allocator<_CharT> >(__zero, __one);
381 std::basic_string<char, std::char_traits<char>, std::allocator<char> >
384 return to_string<char,std::char_traits<char>,std::allocator<char> >();
388 std::basic_string<char, std::char_traits<char>, std::allocator<char> >
389 to_string(char __zero, char __one = '1') const
391 return to_string<char, std::char_traits<char>,
392 std::allocator<char> >(__zero, __one);
400 operator==(const bitset<_Nb>& __rhs) const _GLIBCXX_NOEXCEPT
401 {
return _M_base() == __rhs._M_base(); }
403#if __cpp_impl_three_way_comparison < 201907L
405 operator!=(const bitset<_Nb>& __rhs) const _GLIBCXX_NOEXCEPT
406 {
return _M_base() != __rhs._M_base(); }
416 operator<<(size_t __pos) const _GLIBCXX_NOEXCEPT
417 {
return bitset<_Nb>(_M_base() << __pos); }
421 operator>>(size_t __pos) const _GLIBCXX_NOEXCEPT
422 {
return bitset<_Nb>(_M_base() >> __pos); }
426 _M_base() _GLIBCXX_NOEXCEPT
431 _M_base() const _GLIBCXX_NOEXCEPT
438 operator&(const bitset<_Nb>& __x, const bitset<_Nb>& __y) _GLIBCXX_NOEXCEPT
439 {
return bitset<_Nb>(__x) &= __y; }
444 operator|(const bitset<_Nb>& __x, const bitset<_Nb>& __y) _GLIBCXX_NOEXCEPT
445 {
return bitset<_Nb>(__x) |= __y; }
450 operator^(const bitset<_Nb>& __x, const bitset<_Nb>& __y) _GLIBCXX_NOEXCEPT
451 {
return bitset<_Nb>(__x) ^= __y; }
453 template<typename _CharT, typename _Traits, size_t _Nb>
454 inline std::basic_istream<_CharT, _Traits>&
455 operator>>(std::basic_istream<_CharT, _Traits>& __is, bitset<_Nb>& __x)
456 {
return __is >> __x._M_base(); }
458 template<typename _CharT, typename _Traits, size_t _Nb>
459 inline std::basic_ostream<_CharT, _Traits>&
460 operator<<(std::basic_ostream<_CharT, _Traits>& __os,
461 const bitset<_Nb>& __x)
462 {
return __os << __x._M_base(); }
464}
// namespace __debug
466#if __cplusplus >= 201103L
468 /// std::hash specialization for bitset.
470 struct hash<__debug::bitset<_Nb>>
471 : public __hash_base<size_t, __debug::bitset<_Nb>>
474 operator()(const __debug::bitset<_Nb>& __b) const noexcept
475 {
return std::hash<_GLIBCXX_STD_C::bitset<_Nb>>()(__b._M_base()); }