libstdc++
debug/bitset
Go to the documentation of this file.
1 // Debugging bitset implementation -*- C++ -*-
2 
3 // Copyright (C) 2003-2026 Free Software Foundation, Inc.
4 //
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)
9 // any later version.
10 
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.
15 
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.
19 
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/>.
24 
25 /** @file debug/bitset
26  * This file is a GNU debug extension to the Standard C++ Library.
27  */
28 
29 #ifndef _GLIBCXX_DEBUG_BITSET
30 #define _GLIBCXX_DEBUG_BITSET
31 
32 #ifdef _GLIBCXX_SYSHDR
33 #pragma GCC system_header
34 #endif
35 
36 #include <bitset>
37 #include <debug/safe_sequence.h>
38 #include <debug/safe_iterator.h>
39 
40 namespace std _GLIBCXX_VISIBILITY(default)
41 {
42 namespace __debug
43 {
44  /// Class std::bitset with additional safety/checking/debug instrumentation.
45  template<size_t _Nb>
46  class bitset
47  : public _GLIBCXX_STD_C::bitset<_Nb>
48 #if __cplusplus < 201103L
50 #endif
51  {
52  typedef _GLIBCXX_STD_C::bitset<_Nb> _Base;
53 
54  public:
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;
60 #else
61  // bit reference:
62  class reference
63  : private _Base::reference
65  {
66  typedef typename _Base::reference _Base_ref;
67 
68  friend class bitset;
69  reference();
70 
71  reference(const _Base_ref& __base, bitset* __seq) _GLIBCXX_NOEXCEPT
72  : _Base_ref(__base)
73  , _Safe_iterator_base(__seq, false)
74  { }
75 
76  public:
77  reference(const reference& __x) _GLIBCXX_NOEXCEPT
78  : _Base_ref(__x)
79  , _Safe_iterator_base(__x, false)
80  { }
81 
82  reference&
83  operator=(bool __x) _GLIBCXX_NOEXCEPT
84  {
85  _GLIBCXX_DEBUG_VERIFY(!this->_M_singular(),
86  _M_message(__gnu_debug::__msg_bad_bitset_write)
87  ._M_iterator(*this));
88  *static_cast<_Base_ref*>(this) = __x;
89  return *this;
90  }
91 
92  reference&
93  operator=(const reference& __x) _GLIBCXX_NOEXCEPT
94  {
95  _GLIBCXX_DEBUG_VERIFY(!__x._M_singular(),
96  _M_message(__gnu_debug::__msg_bad_bitset_read)
97  ._M_iterator(__x));
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;
102  return *this;
103  }
104 
105  bool
106  operator~() const _GLIBCXX_NOEXCEPT
107  {
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));
112  }
113 
114  operator bool() const _GLIBCXX_NOEXCEPT
115  {
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);
120  }
121 
122  reference&
123  flip() _GLIBCXX_NOEXCEPT
124  {
125  _GLIBCXX_DEBUG_VERIFY(!this->_M_singular(),
126  _M_message(__gnu_debug::__msg_bad_bitset_flip)
127  ._M_iterator(*this));
128  _Base_ref::flip();
129  return *this;
130  }
131  };
132 #endif
133 
134  // 23.3.5.1 constructors:
135  _GLIBCXX_CONSTEXPR bitset() _GLIBCXX_NOEXCEPT
136  : _Base() { }
137 
138 #if __cplusplus >= 201103L
139  constexpr bitset(unsigned long long __val) noexcept
140 #else
141  bitset(unsigned long __val)
142 #endif
143  : _Base(__val) { }
144 
145  template<typename _CharT, typename _Traits, typename _Alloc>
146  _GLIBCXX23_CONSTEXPR
147  explicit
149  typename std::basic_string<_CharT, _Traits, _Alloc>::size_type
150  __pos = 0,
151  typename std::basic_string<_CharT, _Traits, _Alloc>::size_type
153  : _Base(__str, __pos, __n) { }
154 
155  // _GLIBCXX_RESOLVE_LIB_DEFECTS
156  // 396. what are characters zero and one.
157  template<class _CharT, class _Traits, class _Alloc>
158  _GLIBCXX23_CONSTEXPR
159  bitset(const std::basic_string<_CharT, _Traits, _Alloc>& __str,
160  typename std::basic_string<_CharT, _Traits, _Alloc>::size_type
161  __pos,
162  typename std::basic_string<_CharT, _Traits, _Alloc>::size_type
163  __n,
164  _CharT __zero, _CharT __one = _CharT('1'))
165  : _Base(__str, __pos, __n, __zero, __one) { }
166 
167 #ifdef __cpp_lib_bitset // ... from string_view
168  template<class _CharT, class _Traits>
169  constexpr explicit
171  std::basic_string_view<_CharT, _Traits>::size_type __position = 0,
172  std::basic_string_view<_CharT, _Traits>::size_type __n =
174  _CharT __zero = _CharT('0'), _CharT __one = _CharT('1'))
175  : _Base(__s, __position, __n, __zero, __one) { }
176 #endif
177 
178  _GLIBCXX23_CONSTEXPR
179  bitset(const _Base& __x) : _Base(__x) { }
180 
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>,
188  __not_<is_array<_CharT>>>>
189  _GLIBCXX23_CONSTEXPR
190  explicit
191  bitset(const _CharT* __str,
192  typename std::basic_string<_CharT>::size_type __n
194  _CharT __zero = _CharT('0'), _CharT __one = _CharT('1'))
195  : _Base(__str, __n, __zero, __one) { }
196 #endif
197 
198  // 23.3.5.2 bitset operations:
199  _GLIBCXX23_CONSTEXPR
200  bitset<_Nb>&
201  operator&=(const bitset<_Nb>& __rhs) _GLIBCXX_NOEXCEPT
202  {
203  _M_base() &= __rhs;
204  return *this;
205  }
206 
207  _GLIBCXX23_CONSTEXPR
208  bitset<_Nb>&
209  operator|=(const bitset<_Nb>& __rhs) _GLIBCXX_NOEXCEPT
210  {
211  _M_base() |= __rhs;
212  return *this;
213  }
214 
215  _GLIBCXX23_CONSTEXPR
216  bitset<_Nb>&
217  operator^=(const bitset<_Nb>& __rhs) _GLIBCXX_NOEXCEPT
218  {
219  _M_base() ^= __rhs;
220  return *this;
221  }
222 
223  _GLIBCXX23_CONSTEXPR
224  bitset<_Nb>&
225  operator<<=(size_t __pos) _GLIBCXX_NOEXCEPT
226  {
227  _M_base() <<= __pos;
228  return *this;
229  }
230 
231  _GLIBCXX23_CONSTEXPR
232  bitset<_Nb>&
233  operator>>=(size_t __pos) _GLIBCXX_NOEXCEPT
234  {
235  _M_base() >>= __pos;
236  return *this;
237  }
238 
239  _GLIBCXX23_CONSTEXPR
240  bitset<_Nb>&
241  set() _GLIBCXX_NOEXCEPT
242  {
243  _Base::set();
244  return *this;
245  }
246 
247  // _GLIBCXX_RESOLVE_LIB_DEFECTS
248  // 186. bitset::set() second parameter should be bool
249  _GLIBCXX23_CONSTEXPR
250  bitset<_Nb>&
251  set(size_t __pos, bool __val = true)
252  {
253  _Base::set(__pos, __val);
254  return *this;
255  }
256 
257  _GLIBCXX23_CONSTEXPR
258  bitset<_Nb>&
259  reset() _GLIBCXX_NOEXCEPT
260  {
261  _Base::reset();
262  return *this;
263  }
264 
265  _GLIBCXX23_CONSTEXPR
266  bitset<_Nb>&
267  reset(size_t __pos)
268  {
269  _Base::reset(__pos);
270  return *this;
271  }
272 
273  _GLIBCXX23_CONSTEXPR
275  operator~() const _GLIBCXX_NOEXCEPT
276  { return bitset(~_M_base()); }
277 
278  _GLIBCXX23_CONSTEXPR
279  bitset<_Nb>&
280  flip() _GLIBCXX_NOEXCEPT
281  {
282  _Base::flip();
283  return *this;
284  }
285 
286  _GLIBCXX23_CONSTEXPR
287  bitset<_Nb>&
288  flip(size_t __pos)
289  {
290  _Base::flip(__pos);
291  return *this;
292  }
293 
294  // element access:
295  // _GLIBCXX_RESOLVE_LIB_DEFECTS
296  // 11. Bitset minor problems
297  _GLIBCXX23_CONSTEXPR
298  reference
299  operator[](size_t __pos)
300  {
301  __glibcxx_check_subscript(__pos);
302 #if __cplusplus >= 201103L
303  return _M_base()[__pos];
304 #else
305  return reference(_M_base()[__pos], this);
306 #endif
307  }
308 
309  // _GLIBCXX_RESOLVE_LIB_DEFECTS
310  // 11. Bitset minor problems
311  _GLIBCXX_CONSTEXPR bool
312  operator[](size_t __pos) const
313  {
314 #if __cplusplus < 201103L
315  // TODO: Check in debug-mode too.
316  __glibcxx_check_subscript(__pos);
317 #endif
318  return _Base::operator[](__pos);
319  }
320 
321  using _Base::to_ulong;
322 #if __cplusplus >= 201103L
323  using _Base::to_ullong;
324 #endif
325 
326  template <typename _CharT, typename _Traits, typename _Alloc>
327  _GLIBCXX23_CONSTEXPR
329  to_string() const
330  { return _M_base().template to_string<_CharT, _Traits, _Alloc>(); }
331 
332  // _GLIBCXX_RESOLVE_LIB_DEFECTS
333  // 396. what are characters zero and one.
334  template<class _CharT, class _Traits, class _Alloc>
335  _GLIBCXX23_CONSTEXPR
337  to_string(_CharT __zero, _CharT __one = _CharT('1')) const
338  {
339  return _M_base().template
340  to_string<_CharT, _Traits, _Alloc>(__zero, __one);
341  }
342 
343  // _GLIBCXX_RESOLVE_LIB_DEFECTS
344  // 434. bitset::to_string() hard to use.
345  template<typename _CharT, typename _Traits>
346  _GLIBCXX23_CONSTEXPR
348  to_string() const
349  { return to_string<_CharT, _Traits, std::allocator<_CharT> >(); }
350 
351  // _GLIBCXX_RESOLVE_LIB_DEFECTS
352  // 853. to_string needs updating with zero and one.
353  template<class _CharT, class _Traits>
354  _GLIBCXX23_CONSTEXPR
356  to_string(_CharT __zero, _CharT __one = _CharT('1')) const
357  { return to_string<_CharT, _Traits,
358  std::allocator<_CharT> >(__zero, __one); }
359 
360  template<typename _CharT>
361  _GLIBCXX23_CONSTEXPR
364  to_string() const
365  {
366  return to_string<_CharT, std::char_traits<_CharT>,
368  }
369 
370  template<class _CharT>
371  _GLIBCXX23_CONSTEXPR
372  std::basic_string<_CharT, std::char_traits<_CharT>,
374  to_string(_CharT __zero, _CharT __one = _CharT('1')) const
375  {
376  return to_string<_CharT, std::char_traits<_CharT>,
377  std::allocator<_CharT> >(__zero, __one);
378  }
379 
380  _GLIBCXX23_CONSTEXPR
382  to_string() const
383  {
384  return to_string<char,std::char_traits<char>,std::allocator<char> >();
385  }
386 
387  _GLIBCXX23_CONSTEXPR
388  std::basic_string<char, std::char_traits<char>, std::allocator<char> >
389  to_string(char __zero, char __one = '1') const
390  {
391  return to_string<char, std::char_traits<char>,
392  std::allocator<char> >(__zero, __one);
393  }
394 
395  using _Base::count;
396  using _Base::size;
397 
398  _GLIBCXX23_CONSTEXPR
399  bool
400  operator==(const bitset<_Nb>& __rhs) const _GLIBCXX_NOEXCEPT
401  { return _M_base() == __rhs._M_base(); }
402 
403 #if __cpp_impl_three_way_comparison < 201907L
404  bool
405  operator!=(const bitset<_Nb>& __rhs) const _GLIBCXX_NOEXCEPT
406  { return _M_base() != __rhs._M_base(); }
407 #endif
408 
409  using _Base::test;
410  using _Base::all;
411  using _Base::any;
412  using _Base::none;
413 
414  _GLIBCXX23_CONSTEXPR
416  operator<<(size_t __pos) const _GLIBCXX_NOEXCEPT
417  { return bitset<_Nb>(_M_base() << __pos); }
418 
419  _GLIBCXX23_CONSTEXPR
421  operator>>(size_t __pos) const _GLIBCXX_NOEXCEPT
422  { return bitset<_Nb>(_M_base() >> __pos); }
423 
424  _GLIBCXX23_CONSTEXPR
425  _Base&
426  _M_base() _GLIBCXX_NOEXCEPT
427  { return *this; }
428 
429  _GLIBCXX23_CONSTEXPR
430  const _Base&
431  _M_base() const _GLIBCXX_NOEXCEPT
432  { return *this; }
433  };
434 
435  template<size_t _Nb>
436  _GLIBCXX23_CONSTEXPR
437  inline bitset<_Nb>
438  operator&(const bitset<_Nb>& __x, const bitset<_Nb>& __y) _GLIBCXX_NOEXCEPT
439  { return bitset<_Nb>(__x) &= __y; }
440 
441  template<size_t _Nb>
442  _GLIBCXX23_CONSTEXPR
443  inline bitset<_Nb>
444  operator|(const bitset<_Nb>& __x, const bitset<_Nb>& __y) _GLIBCXX_NOEXCEPT
445  { return bitset<_Nb>(__x) |= __y; }
446 
447  template<size_t _Nb>
448  _GLIBCXX23_CONSTEXPR
449  inline bitset<_Nb>
450  operator^(const bitset<_Nb>& __x, const bitset<_Nb>& __y) _GLIBCXX_NOEXCEPT
451  { return bitset<_Nb>(__x) ^= __y; }
452 
453  template<typename _CharT, typename _Traits, size_t _Nb>
456  { return __is >> __x._M_base(); }
457 
458  template<typename _CharT, typename _Traits, size_t _Nb>
460  operator<<(std::basic_ostream<_CharT, _Traits>& __os,
461  const bitset<_Nb>& __x)
462  { return __os << __x._M_base(); }
463 
464 } // namespace __debug
465 
466 #if __cplusplus >= 201103L
467  // DR 1182.
468  /// std::hash specialization for bitset.
469  template<size_t _Nb>
470  struct hash<__debug::bitset<_Nb>>
471  : public __hash_base<size_t, __debug::bitset<_Nb>>
472  {
473  size_t
474  operator()(const __debug::bitset<_Nb>& __b) const noexcept
475  { return std::hash<_GLIBCXX_STD_C::bitset<_Nb>>()(__b._M_base()); }
476  };
477 #endif
478 
479 } // namespace std
480 
481 #endif
is_trivially_default_constructible
Definition: type_traits:1434
Class std::bitset with additional safety/checking/debug instrumentation.
Definition: debug/bitset:46
constexpr bitset< _Nb > operator &(const bitset< _Nb > &__x, const bitset< _Nb > &__y) noexcept
Global bitwise operations on bitsets.
Definition: bitset:1618
std::basic_ostream< _CharT, _Traits > & operator<<(std::basic_ostream< _CharT, _Traits > &__os, const bitset< _Nb > &__x)
Global I/O operators for bitsets.
Definition: bitset:1754
Base class that supports tracking of iterators that reference a sequence.
Definition: safe_base.h:218
The bitset class represents a fixed-size sequence of bits.(Note that bitset does not meet the formal ...
Definition: bitset:820
constexpr bitset< _Nb > operator^(const bitset< _Nb > &__x, const bitset< _Nb > &__y) noexcept
Global bitwise operations on bitsets.
Definition: bitset:1638
Template class basic_ostream.
Definition: iosfwd:90
ISO C++ entities toplevel namespace is std.
Basic functionality for a safe iterator.
Definition: safe_base.h:50
A non-owning reference to a string.
Definition: string_view:112
std::basic_istream< _CharT, _Traits > & operator>>(std::basic_istream< _CharT, _Traits > &__is, bitset< _Nb > &__x)
Global I/O operators for bitsets.
Definition: bitset:1658
is_standard_layout
Definition: type_traits:978
Template class basic_istream.
Definition: iosfwd:87
The standard allocator, as per C++03 [20.4.1].
Definition: allocator.h:133
Primary class template hash.
Definition: string_view:796
constexpr _Iterator __base(_Iterator __it)
constexpr bitset< _Nb > operator|(const bitset< _Nb > &__x, const bitset< _Nb > &__y) noexcept
Global bitwise operations on bitsets.
Definition: bitset:1628
Managing sequences of characters and character-like objects.
Definition: cow_string.h:108