@@ -22,12 +22,13 @@ _STL_DISABLE_CLANG_WARNINGS
22
22
_STD_BEGIN
23
23
_EXPORT_STD template <size_t _Bits>
24
24
class bitset { // store fixed-length sequence of Boolean elements
25
- public :
25
+ private :
26
26
#pragma warning(push)
27
27
#pragma warning(disable : 4296) // expression is always true (/Wall)
28
28
using _Ty = conditional_t<_Bits <= sizeof(unsigned long) * CHAR_BIT, unsigned long, unsigned long long>;
29
29
#pragma warning(pop)
30
30
31
+ public:
31
32
class reference { // proxy for an element
32
33
friend bitset;
33
34
@@ -68,25 +69,26 @@ public:
68
69
size_t _Mypos; // position of element in bitset
69
70
};
70
71
71
- static _CONSTEXPR23 void _Validate(const size_t _Pos) noexcept { // verify that _Pos is within bounds
72
+ private:
73
+ static constexpr void _Validate(const size_t _Pos) noexcept { // verify that _Pos is within bounds
72
74
#if _ITERATOR_DEBUG_LEVEL == 0
73
75
(void) _Pos;
74
76
#else // ^^^ _ITERATOR_DEBUG_LEVEL == 0 / _ITERATOR_DEBUG_LEVEL != 0 vvv
75
77
_STL_VERIFY(_Pos < _Bits, "bitset index outside range");
76
78
#endif // _ITERATOR_DEBUG_LEVEL == 0
77
79
}
78
80
79
- constexpr bool _Subscript(size_t _Pos) const {
81
+ constexpr bool _Subscript(size_t _Pos) const noexcept {
80
82
return (_Array[_Pos / _Bitsperword] & (_Ty{1} << _Pos % _Bitsperword)) != 0;
81
83
}
82
84
83
- _NODISCARD constexpr bool operator[](size_t _Pos) const {
84
- #if _ITERATOR_DEBUG_LEVEL == 0
85
- return _Subscript(_Pos);
85
+ static constexpr bool _Need_mask = _Bits < CHAR_BIT * sizeof(unsigned long long);
86
+ static constexpr unsigned long long _Mask = (1ULL << (_Need_mask ? _Bits : 0)) - 1ULL;
86
87
87
- #else // _ITERATOR_DEBUG_LEVEL == 0
88
- return _Bits <= _Pos ? (_Validate(_Pos), false) : _Subscript(_Pos);
89
- #endif // _ITERATOR_DEBUG_LEVEL == 0
88
+ public:
89
+ _NODISCARD constexpr bool operator[](size_t _Pos) const noexcept /* strengthened */ {
90
+ _Validate(_Pos);
91
+ return _Subscript(_Pos);
90
92
}
91
93
92
94
_NODISCARD _CONSTEXPR23 reference operator[](const size_t _Pos) noexcept /* strengthened */ {
@@ -96,12 +98,9 @@ public:
96
98
97
99
constexpr bitset() noexcept : _Array() {} // construct with all false values
98
100
99
- static constexpr bool _Need_mask = _Bits < CHAR_BIT * sizeof(unsigned long long);
100
-
101
- static constexpr unsigned long long _Mask = (1ULL << (_Need_mask ? _Bits : 0)) - 1ULL;
102
-
103
101
constexpr bitset(unsigned long long _Val) noexcept : _Array{static_cast<_Ty>(_Need_mask ? _Val & _Mask : _Val)} {}
104
102
103
+ private:
105
104
template <class _Traits, class _Elem>
106
105
_CONSTEXPR23 void _Construct(const _Elem* const _Ptr, size_t _Count, const _Elem _Elem0, const _Elem _Elem1) {
107
106
if (_Count > _Bits) {
@@ -147,6 +146,7 @@ public:
147
146
}
148
147
}
149
148
149
+ public:
150
150
template <class _Elem, class _Traits, class _Alloc>
151
151
_CONSTEXPR23 explicit bitset(const basic_string<_Elem, _Traits, _Alloc>& _Str,
152
152
typename basic_string<_Elem, _Traits, _Alloc>::size_type _Pos = 0,
@@ -347,11 +347,11 @@ public:
347
347
_NODISCARD _CONSTEXPR23 basic_string<_Elem, _Tr, _Alloc> to_string(
348
348
const _Elem _Elem0 = static_cast<_Elem>('0'), const _Elem _Elem1 = static_cast<_Elem>('1')) const {
349
349
// convert bitset to string
350
- basic_string<_Elem, _Tr, _Alloc> _Str;
351
- _Str.reserve( _Bits);
352
-
353
- for (auto _Pos = _Bits; 0 < _Pos;) {
354
- _Str.push_back(_Subscript(--_Pos) ? _Elem1 : _Elem0);
350
+ basic_string<_Elem, _Tr, _Alloc> _Str(_Bits, _Elem0) ;
351
+ for (size_t _Pos = 0; _Pos < _Bits; ++_Pos) {
352
+ if (_Subscript(_Bits - 1 - _Pos)) {
353
+ _Str[ _Pos] = _Elem1;
354
+ }
355
355
}
356
356
357
357
return _Str;
0 commit comments