Skip to content

Commit b5fa141

Browse files
committed
[libc++] Always return bool from bitset::operator[](size_t) const
1 parent 8bfca26 commit b5fa141

File tree

3 files changed

+14
-36
lines changed

3 files changed

+14
-36
lines changed

libcxx/include/__cxx03/bitset

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -612,11 +612,7 @@ public:
612612
_LIBCPP_HIDE_FROM_ABI bitset& flip(size_t __pos);
613613

614614
// element access:
615-
#ifdef _LIBCPP_ABI_BITSET_VECTOR_BOOL_CONST_SUBSCRIPT_RETURN_BOOL
616615
_LIBCPP_HIDE_FROM_ABI bool operator[](size_t __p) const { return __base::__make_ref(__p); }
617-
#else
618-
_LIBCPP_HIDE_FROM_ABI __const_reference operator[](size_t __p) const { return __base::__make_ref(__p); }
619-
#endif
620616
_LIBCPP_HIDE_FROM_ABI reference operator[](size_t __p) { return __base::__make_ref(__p); }
621617
_LIBCPP_HIDE_FROM_ABI unsigned long to_ulong() const;
622618
_LIBCPP_HIDE_FROM_ABI unsigned long long to_ullong() const;

libcxx/include/bitset

Lines changed: 14 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -151,6 +151,7 @@ template <size_t N> struct hash<std::bitset<N>>;
151151
# include <__type_traits/integral_constant.h>
152152
# include <__type_traits/is_char_like_type.h>
153153
# include <__utility/integer_sequence.h>
154+
# include <__utility/unreachable.h>
154155
# include <climits>
155156
# include <stdexcept>
156157
# include <string_view>
@@ -191,15 +192,13 @@ protected:
191192
static const unsigned __bits_per_word = static_cast<unsigned>(sizeof(__storage_type) * CHAR_BIT);
192193

193194
friend class __bit_reference<__bitset>;
194-
friend class __bit_const_reference<__bitset>;
195195
friend class __bit_iterator<__bitset, false>;
196196
friend class __bit_iterator<__bitset, true>;
197197
friend struct __bit_array<__bitset>;
198198

199199
__storage_type __first_[_N_words];
200200

201201
typedef __bit_reference<__bitset> reference;
202-
typedef __bit_const_reference<__bitset> __const_reference;
203202
typedef __bit_iterator<__bitset, false> __iterator;
204203
typedef __bit_iterator<__bitset, true> __const_iterator;
205204

@@ -209,16 +208,17 @@ protected:
209208
_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX23 reference __make_ref(size_t __pos) _NOEXCEPT {
210209
return reference(__first_ + __pos / __bits_per_word, __storage_type(1) << __pos % __bits_per_word);
211210
}
212-
_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR __const_reference __make_ref(size_t __pos) const _NOEXCEPT {
213-
return __const_reference(__first_ + __pos / __bits_per_word, __storage_type(1) << __pos % __bits_per_word);
214-
}
215211
_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX23 __iterator __make_iter(size_t __pos) _NOEXCEPT {
216212
return __iterator(__first_ + __pos / __bits_per_word, __pos % __bits_per_word);
217213
}
218214
_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX23 __const_iterator __make_iter(size_t __pos) const _NOEXCEPT {
219215
return __const_iterator(__first_ + __pos / __bits_per_word, __pos % __bits_per_word);
220216
}
221217

218+
_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX23 bool __is_set(size_t __pos) const _NOEXCEPT {
219+
return __first_[__pos / __bits_per_word] & (__storage_type(1) << __pos % __bits_per_word);
220+
}
221+
222222
_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX23 void operator&=(const __bitset& __v) _NOEXCEPT;
223223
_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX23 void operator|=(const __bitset& __v) _NOEXCEPT;
224224
_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX23 void operator^=(const __bitset& __v) _NOEXCEPT;
@@ -416,15 +416,13 @@ protected:
416416
static const unsigned __bits_per_word = static_cast<unsigned>(sizeof(__storage_type) * CHAR_BIT);
417417

418418
friend class __bit_reference<__bitset>;
419-
friend class __bit_const_reference<__bitset>;
420419
friend class __bit_iterator<__bitset, false>;
421420
friend class __bit_iterator<__bitset, true>;
422421
friend struct __bit_array<__bitset>;
423422

424423
__storage_type __first_;
425424

426425
typedef __bit_reference<__bitset> reference;
427-
typedef __bit_const_reference<__bitset> __const_reference;
428426
typedef __bit_iterator<__bitset, false> __iterator;
429427
typedef __bit_iterator<__bitset, true> __const_iterator;
430428

@@ -434,9 +432,6 @@ protected:
434432
_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX23 reference __make_ref(size_t __pos) _NOEXCEPT {
435433
return reference(&__first_, __storage_type(1) << __pos);
436434
}
437-
_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR __const_reference __make_ref(size_t __pos) const _NOEXCEPT {
438-
return __const_reference(&__first_, __storage_type(1) << __pos);
439-
}
440435
_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX23 __iterator __make_iter(size_t __pos) _NOEXCEPT {
441436
// Allow the == case to accommodate the past-the-end iterator.
442437
_LIBCPP_ASSERT_INTERNAL(__pos <= __bits_per_word, "Out of bounds access in the single-word bitset implementation.");
@@ -448,6 +443,10 @@ protected:
448443
return __pos != __bits_per_word ? __const_iterator(&__first_, __pos) : __const_iterator(&__first_ + 1, 0);
449444
}
450445

446+
_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX23 bool __is_set(size_t __pos) const _NOEXCEPT {
447+
return __first_ & (__storage_type(1) << __pos);
448+
}
449+
451450
_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX23 void operator&=(const __bitset& __v) _NOEXCEPT;
452451
_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX23 void operator|=(const __bitset& __v) _NOEXCEPT;
453452
_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX23 void operator^=(const __bitset& __v) _NOEXCEPT;
@@ -556,13 +555,11 @@ protected:
556555
static const unsigned __bits_per_word = static_cast<unsigned>(sizeof(__storage_type) * CHAR_BIT);
557556

558557
friend class __bit_reference<__bitset>;
559-
friend class __bit_const_reference<__bitset>;
560558
friend class __bit_iterator<__bitset, false>;
561559
friend class __bit_iterator<__bitset, true>;
562560
friend struct __bit_array<__bitset>;
563561

564562
typedef __bit_reference<__bitset> reference;
565-
typedef __bit_const_reference<__bitset> __const_reference;
566563
typedef __bit_iterator<__bitset, false> __iterator;
567564
typedef __bit_iterator<__bitset, true> __const_iterator;
568565

@@ -572,16 +569,17 @@ protected:
572569
_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX23 reference __make_ref(size_t) _NOEXCEPT {
573570
return reference(nullptr, 1);
574571
}
575-
_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR __const_reference __make_ref(size_t) const _NOEXCEPT {
576-
return __const_reference(nullptr, 1);
577-
}
578572
_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX23 __iterator __make_iter(size_t) _NOEXCEPT {
579573
return __iterator(nullptr, 0);
580574
}
581575
_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX23 __const_iterator __make_iter(size_t) const _NOEXCEPT {
582576
return __const_iterator(nullptr, 0);
583577
}
584578

579+
_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX23 bool __is_set(size_t) const _NOEXCEPT {
580+
std::__libcpp_unreachable();
581+
}
582+
585583
_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX23 void operator&=(const __bitset&) _NOEXCEPT {}
586584
_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX23 void operator|=(const __bitset&) _NOEXCEPT {}
587585
_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX23 void operator^=(const __bitset&) _NOEXCEPT {}
@@ -618,7 +616,6 @@ public:
618616
static const unsigned __n_words = _Size == 0 ? 0 : (_Size - 1) / (sizeof(size_t) * CHAR_BIT) + 1;
619617
typedef __bitset<__n_words, _Size> __base;
620618
typedef typename __base::reference reference;
621-
typedef typename __base::__const_reference __const_reference;
622619

623620
// 23.3.5.1 constructors:
624621
_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR bitset() _NOEXCEPT {}
@@ -680,17 +677,10 @@ public:
680677
_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX23 bitset& flip(size_t __pos);
681678

682679
// element access:
683-
# ifdef _LIBCPP_ABI_BITSET_VECTOR_BOOL_CONST_SUBSCRIPT_RETURN_BOOL
684680
_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR bool operator[](size_t __p) const {
685681
_LIBCPP_ASSERT_VALID_ELEMENT_ACCESS(__p < _Size, "bitset::operator[] index out of bounds");
686-
return __base::__make_ref(__p);
682+
return __base::__is_set(__p);
687683
}
688-
# else
689-
_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR __const_reference operator[](size_t __p) const {
690-
_LIBCPP_ASSERT_VALID_ELEMENT_ACCESS(__p < _Size, "bitset::operator[] index out of bounds");
691-
return __base::__make_ref(__p);
692-
}
693-
# endif
694684
_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX23 reference operator[](size_t __p) {
695685
_LIBCPP_ASSERT_VALID_ELEMENT_ACCESS(__p < _Size, "bitset::operator[] index out of bounds");
696686
return __base::__make_ref(__p);

libcxx/test/std/utilities/template.bitset/bitset.members/index_const.pass.cpp

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -25,11 +25,7 @@ TEST_CONSTEXPR_CXX23 void test_index_const() {
2525
assert(v[N / 2] == v.test(N / 2));
2626
}
2727
}
28-
#if !defined(_LIBCPP_VERSION) || defined(_LIBCPP_ABI_BITSET_VECTOR_BOOL_CONST_SUBSCRIPT_RETURN_BOOL)
2928
ASSERT_SAME_TYPE(decltype(cases[0][0]), bool);
30-
#else
31-
ASSERT_SAME_TYPE(decltype(cases[0][0]), typename std::bitset<N>::__const_reference);
32-
#endif
3329
}
3430

3531
TEST_CONSTEXPR_CXX23 bool test() {
@@ -47,11 +43,7 @@ TEST_CONSTEXPR_CXX23 bool test() {
4743
const auto& set = set_;
4844
auto b = set[0];
4945
set_[0] = true;
50-
#if !defined(_LIBCPP_VERSION) || defined(_LIBCPP_ABI_BITSET_VECTOR_BOOL_CONST_SUBSCRIPT_RETURN_BOOL)
5146
assert(!b);
52-
#else
53-
assert(b);
54-
#endif
5547

5648
return true;
5749
}

0 commit comments

Comments
 (0)