_GLIBCXX23_CONSTEXPR
reference
operator[](size_t __position)
- { return reference(*this, __position); }
+ {
+ __glibcxx_assert(__position < _Nb);
+ return reference(*this, __position);
+ }
_GLIBCXX_CONSTEXPR bool
operator[](size_t __position) const
- { return _Unchecked_test(__position); }
+ {
+ __glibcxx_assert(__position < _Nb);
+ return _Unchecked_test(__position);
+ }
///@}
/**
--- /dev/null
+// { dg-do run { xfail *-*-* } }
+// { dg-options "-D_GLIBCXX_ASSERTIONS" }
+
+#include <bitset>
+#include <testsuite_hooks.h>
+
+// Check bitset<>::op[] hardening, non-const.
+
+int main()
+{
+ std::bitset<13> bs(0x1555ull);
+ bs[12]; // OK
+ bs[13]; // aborts, 13 > 12, non-const
+}
--- /dev/null
+// { dg-do run { xfail *-*-* } }
+// { dg-options "-D_GLIBCXX_ASSERTIONS" }
+
+#include <bitset>
+#include <testsuite_hooks.h>
+
+// Check bitset<>::op[] hardening, const.
+
+int main()
+{
+ const std::bitset<13> bs(0x1555ull);
+ bs[12]; // OK
+ bs[13]; // aborts, 13 > 12, const
+}
--- /dev/null
+// { dg-do run }
+// { dg-options "-D_GLIBCXX_ASSERTIONS" }
+
+// Smoke test, op[] hardening.
+
+#include <bitset>
+#include <testsuite_hooks.h>
+
+void test_non_const_subscript()
+{
+ std::bitset<13> bs(0x1555ull);
+ for (int i = 0; i < 13; ++i)
+ {
+ VERIFY(bs[i] != (i & 1)); // Check op[] proxy result rvalue.
+ bs[i] = not bs[i]; // Assign via op[] proxy result lvalue.
+ VERIFY(bs[i] == (i & 1)); // Check modified.
+ }
+}
+
+void test_const_subscript()
+{
+ const std::bitset<13> cbs(0x1555ull);
+ for (int i = 0; i < 13; ++i)
+ VERIFY(cbs[i] != (i & 1)); // Check op[] proxy result const rvalue.
+}
+
+int main()
+{
+ test_non_const_subscript();
+ test_const_subscript();
+}
--- /dev/null
+#include <bitset>
+#include <testsuite_hooks.h>
+
+void test_non_const_subscript()
+{
+ std::bitset<13> bs(0x1555ull);
+ for (int i = 0; i < 13; ++i)
+ {
+ VERIFY(bs[i] != (i & 1)); // Check op[] proxy result rvalue.
+ bs[i] = not bs[i]; // Assign via op[] proxy result lvalue.
+ VERIFY(bs[i] == (i & 1)); // Check modified.
+ }
+}
+
+void test_const_subscript()
+{
+ const std::bitset<13> cbs(0x1555ull);
+ for (int i = 0; i < 13; ++i)
+ VERIFY(cbs[i] != (i & 1)); // Check op[] proxy result const rvalue.
+}
+
+int main()
+{
+ test_non_const_subscript();
+ test_const_subscript();
+}
--- /dev/null
+#include <bitset>
+
+void test_const_subscript_assignment()
+{
+ const std::bitset<13> bs(0x1555ull);
+ for (int i = 0; i < 13; ++i)
+ bs[i] = not bs[i]; // { dg-error "lvalue required" }
+}
+
+int main()
+{
+ test_const_subscript_assignment();
+}