Skip to content

Commit 3700be1

Browse files
committed
Address further comments by ldionne
1 parent db4565b commit 3700be1

File tree

3 files changed

+12
-34
lines changed

3 files changed

+12
-34
lines changed

libcxx/include/bitset

Lines changed: 10 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -226,29 +226,19 @@ protected:
226226

227227
_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX23 void flip() _NOEXCEPT;
228228

229-
_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX23 unsigned long __to_ulong() const {
229+
_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX23 unsigned long to_ulong() const {
230+
if _LIBCPP_CONSTEXPR (_Size > sizeof(unsigned long) * CHAR_BIT) {
231+
if (auto __e = __make_iter(_Size); std::find(__make_iter(sizeof(unsigned long) * CHAR_BIT), __e, true) != __e)
232+
std::__throw_overflow_error("__bitset<_N_words, _Size>::to_ulong overflow error");
233+
}
234+
230235
static_assert(sizeof(__storage_type) >= sizeof(unsigned long),
231236
"libc++ only supports platforms where sizeof(size_t) >= sizeof(unsigned long), such as 32-bit and "
232237
"64-bit platforms. If you're interested in supporting a platform where that is not the case, please "
233238
"contact the libc++ developers.");
234239
return static_cast<unsigned long>(__first_[0]);
235240
}
236241

237-
// _Bit_size > sizeof(unsigned long) * CHAR_BIT: overflow check needed
238-
template <size_t _Bit_size = _Size, __enable_if_t<(_Bit_size > sizeof(unsigned long) * CHAR_BIT), int> = 0>
239-
_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX23 unsigned long to_ulong() const {
240-
if (auto __e = __make_iter(_Bit_size); std::find(__make_iter(sizeof(unsigned long) * CHAR_BIT), __e, true) != __e)
241-
std::__throw_overflow_error("__bitset<_N_words, _Size>::to_ulong overflow error");
242-
243-
return __to_ulong();
244-
}
245-
246-
// _Bit_size <= sizeof(unsigned long) * CHAR_BIT: no overflow check needed
247-
template <size_t _Bit_size = _Size, __enable_if_t<(_Bit_size <= sizeof(unsigned long) * CHAR_BIT), int> = 0>
248-
_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX23 unsigned long to_ulong() const {
249-
return __to_ulong();
250-
}
251-
252242
_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX23 unsigned long long to_ullong() const {
253243
// Check for overflow if _Size does not fit in unsigned long long
254244
if _LIBCPP_CONSTEXPR (_Size > sizeof(unsigned long long) * CHAR_BIT) {
@@ -466,16 +456,11 @@ protected:
466456

467457
_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX23 void flip() _NOEXCEPT;
468458

469-
template <size_t _Bit_size = _Size, __enable_if_t<(_Bit_size > sizeof(unsigned long) * CHAR_BIT), int> = 0>
470-
_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX23 unsigned long to_ulong() const {
471-
if (auto __e = __make_iter(_Bit_size); std::find(__make_iter(sizeof(unsigned long) * CHAR_BIT), __e, true) != __e)
472-
__throw_overflow_error("__bitset<1, _Size>::to_ulong overflow error");
473-
474-
return static_cast<unsigned long>(__first_);
475-
}
476-
477-
template <size_t _Bit_size = _Size, __enable_if_t<(_Bit_size <= sizeof(unsigned long) * CHAR_BIT), int> = 0>
478459
_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX23 unsigned long to_ulong() const {
460+
if _LIBCPP_CONSTEXPR (_Size > sizeof(unsigned long) * CHAR_BIT) {
461+
if (auto __e = __make_iter(_Size); std::find(__make_iter(sizeof(unsigned long) * CHAR_BIT), __e, true) != __e)
462+
__throw_overflow_error("__bitset<1, _Size>::to_ulong overflow error");
463+
}
479464
return static_cast<unsigned long>(__first_);
480465
}
481466

@@ -519,7 +504,6 @@ inline _LIBCPP_CONSTEXPR __bitset<1, _Size>::__bitset(unsigned long long __v) _N
519504
// TODO: We must refer to __bits_per_word in order to work around an issue with the GDB pretty-printers.
520505
// Without it, the pretty-printers complain about a missing __bits_per_word member. This needs to
521506
// be investigated further.
522-
// See: https://github.com/llvm/llvm-project/actions/runs/15071518915/job/42368867929?pr=121348#logs
523507
: __first_(_Size == __bits_per_word ? static_cast<__storage_type>(__v) : static_cast<__storage_type>(__v)) {}
524508

525509
template <size_t _Size>

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

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,10 +13,7 @@
1313
#include <type_traits>
1414
#include <climits>
1515
#include <cassert>
16-
17-
#ifndef TEST_HAS_NO_EXCEPTIONS
18-
# include <stdexcept>
19-
#endif
16+
#include <stdexcept>
2017

2118
#include "test_macros.h"
2219

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

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,10 +14,7 @@
1414
#include <limits>
1515
#include <climits>
1616
#include <cassert>
17-
18-
#ifndef TEST_HAS_NO_EXCEPTIONS
19-
# include <stdexcept>
20-
#endif
17+
#include <stdexcept>
2118

2219
#include "test_macros.h"
2320

0 commit comments

Comments
 (0)