[libcxx-commits] [libcxx] a34a92d - [libc++] Always return bool from bitset::operator[](size_t) const (#169894)
via libcxx-commits
libcxx-commits at lists.llvm.org
Fri Dec 12 00:06:53 PST 2025
Author: Nikolas Klauser
Date: 2025-12-12T09:06:50+01:00
New Revision: a34a92d9e253473733b955620e80704f0be26f86
URL: https://github.com/llvm/llvm-project/commit/a34a92d9e253473733b955620e80704f0be26f86
DIFF: https://github.com/llvm/llvm-project/commit/a34a92d9e253473733b955620e80704f0be26f86.diff
LOG: [libc++] Always return bool from bitset::operator[](size_t) const (#169894)
This takes an ABI break unconditionally, since it's small enough that
nobody should be affected. This both simplifies `bitset` a bit and makes
us more conforming.
Added:
libcxx/test/libcxx/utilities/template.bitset/index_const.pass.cpp
Modified:
libcxx/docs/ReleaseNotes/22.rst
libcxx/include/__cxx03/bitset
libcxx/include/bitset
libcxx/test/std/utilities/template.bitset/bitset.members/index_const.pass.cpp
Removed:
################################################################################
diff --git a/libcxx/docs/ReleaseNotes/22.rst b/libcxx/docs/ReleaseNotes/22.rst
index ca3c2aad7225f..dc3569d1eeae9 100644
--- a/libcxx/docs/ReleaseNotes/22.rst
+++ b/libcxx/docs/ReleaseNotes/22.rst
@@ -128,5 +128,9 @@ ABI Affecting Changes
``_LIBCPP_DEPRECATED_ABI_NON_TRIVIAL_ALLOCATOR``. Please inform the libc++ team if you need this flag, since it will
be removed in LLVM 24 if there is no evidence that it's required.
+- ``bitset::operator[]`` now returns ``bool``, making libc++ conforming. The behaviour can be reverted by defining
+ ``_LIBCPP_DEPRECATED_ABI_BITSET_CONST_SUBSCRIPT_RETURN_REF``. Please inform the libc++ team if you need this flag,
+ since it will be removed in LLVM 24 if there is no evidence that it's required.
+
Build System Changes
--------------------
diff --git a/libcxx/include/__cxx03/bitset b/libcxx/include/__cxx03/bitset
index 37ad674686ba4..f97431dd5fa09 100644
--- a/libcxx/include/__cxx03/bitset
+++ b/libcxx/include/__cxx03/bitset
@@ -612,7 +612,7 @@ public:
_LIBCPP_HIDE_FROM_ABI bitset& flip(size_t __pos);
// element access:
-#ifdef _LIBCPP_ABI_BITSET_VECTOR_BOOL_CONST_SUBSCRIPT_RETURN_BOOL
+#ifndef _LIBCPP_DEPRECATED_ABI_BITSET_CONST_SUBSCRIPT_RETURN_REF
_LIBCPP_HIDE_FROM_ABI bool operator[](size_t __p) const { return __base::__make_ref(__p); }
#else
_LIBCPP_HIDE_FROM_ABI __const_reference operator[](size_t __p) const { return __base::__make_ref(__p); }
diff --git a/libcxx/include/bitset b/libcxx/include/bitset
index 271e63b8f5764..37253f5722389 100644
--- a/libcxx/include/bitset
+++ b/libcxx/include/bitset
@@ -680,7 +680,8 @@ public:
_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX23 bitset& flip(size_t __pos);
// element access:
-# ifdef _LIBCPP_ABI_BITSET_VECTOR_BOOL_CONST_SUBSCRIPT_RETURN_BOOL
+ // TODO(LLVM 24): Remove the opt-out
+# ifndef _LIBCPP_DEPRECATED_ABI_BITSET_CONST_SUBSCRIPT_RETURN_REF
[[__nodiscard__]] _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR bool operator[](size_t __p) const {
_LIBCPP_ASSERT_VALID_ELEMENT_ACCESS(__p < _Size, "bitset::operator[] index out of bounds");
return __base::__make_ref(__p);
diff --git a/libcxx/test/libcxx/utilities/template.bitset/index_const.pass.cpp b/libcxx/test/libcxx/utilities/template.bitset/index_const.pass.cpp
new file mode 100644
index 0000000000000..4b2334b8a482b
--- /dev/null
+++ b/libcxx/test/libcxx/utilities/template.bitset/index_const.pass.cpp
@@ -0,0 +1,63 @@
+//===----------------------------------------------------------------------===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===----------------------------------------------------------------------===//
+
+// constexpr bool operator[](size_t pos) const; // constexpr since C++23
+
+// Make sure that `_LIBCPP_DEPRECATED_ABI_BITSET_CONST_SUBSCRIPT_RETURN_REF` reverts to the old behaviour.
+
+// ADDITIONAL_COMPILE_FLAGS: -D_LIBCPP_DEPRECATED_ABI_BITSET_CONST_SUBSCRIPT_RETURN_REF
+
+#include <bitset>
+#include <cassert>
+#include <cstddef>
+#include <vector>
+
+#include "../../../std/utilities/template.bitset/bitset_test_cases.h"
+#include "test_macros.h"
+
+template <std::size_t N>
+TEST_CONSTEXPR_CXX23 void test_index_const() {
+ std::vector<std::bitset<N> > const cases = get_test_cases<N>();
+ for (std::size_t c = 0; c != cases.size(); ++c) {
+ std::bitset<N> const v = cases[c];
+ if (v.size() > 0) {
+ assert(v[N / 2] == v.test(N / 2));
+ }
+ }
+ ASSERT_SAME_TYPE(decltype(cases[0][0]), typename std::bitset<N>::__const_reference);
+}
+
+TEST_CONSTEXPR_CXX23 bool test() {
+ test_index_const<0>();
+ test_index_const<1>();
+ test_index_const<31>();
+ test_index_const<32>();
+ test_index_const<33>();
+ test_index_const<63>();
+ test_index_const<64>();
+ test_index_const<65>();
+
+ std::bitset<1> set_;
+ set_[0] = false;
+ const auto& set = set_;
+ auto b = set[0];
+ set_[0] = true;
+ assert(b);
+
+ return true;
+}
+
+int main(int, char**) {
+ test();
+ test_index_const<1000>(); // not in constexpr because of constexpr evaluation step limits
+#if TEST_STD_VER > 20
+ static_assert(test());
+#endif
+
+ return 0;
+}
diff --git a/libcxx/test/std/utilities/template.bitset/bitset.members/index_const.pass.cpp b/libcxx/test/std/utilities/template.bitset/bitset.members/index_const.pass.cpp
index d1bf5b2c5d992..b941a4b2c7074 100644
--- a/libcxx/test/std/utilities/template.bitset/bitset.members/index_const.pass.cpp
+++ b/libcxx/test/std/utilities/template.bitset/bitset.members/index_const.pass.cpp
@@ -25,11 +25,7 @@ TEST_CONSTEXPR_CXX23 void test_index_const() {
assert(v[N / 2] == v.test(N / 2));
}
}
-#if !defined(_LIBCPP_VERSION) || defined(_LIBCPP_ABI_BITSET_VECTOR_BOOL_CONST_SUBSCRIPT_RETURN_BOOL)
ASSERT_SAME_TYPE(decltype(cases[0][0]), bool);
-#else
- ASSERT_SAME_TYPE(decltype(cases[0][0]), typename std::bitset<N>::__const_reference);
-#endif
}
TEST_CONSTEXPR_CXX23 bool test() {
@@ -47,11 +43,7 @@ TEST_CONSTEXPR_CXX23 bool test() {
const auto& set = set_;
auto b = set[0];
set_[0] = true;
-#if !defined(_LIBCPP_VERSION) || defined(_LIBCPP_ABI_BITSET_VECTOR_BOOL_CONST_SUBSCRIPT_RETURN_BOOL)
assert(!b);
-#else
- assert(b);
-#endif
return true;
}
More information about the libcxx-commits
mailing list