Skip to content

Commit 493c066

Browse files
authored
[libc++] Fix ambiguity due to non-uglified member typedefs (#121664)
This PR fixes the ambiguities in name lookup caused by non-standard member typedefs `size_type` and `difference_type` in `std::bitset`. Follows up #121620. Closes #121618.
1 parent f4aec22 commit 493c066

File tree

7 files changed

+68
-29
lines changed

7 files changed

+68
-29
lines changed

libcxx/include/__algorithm/count.h

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ __count(_Iter __first, _Sent __last, const _Tp& __value, _Proj& __proj) {
4444
// __bit_iterator implementation
4545
template <bool _ToCount, class _Cp, bool _IsConst>
4646
_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 typename __bit_iterator<_Cp, _IsConst>::difference_type
47-
__count_bool(__bit_iterator<_Cp, _IsConst> __first, typename _Cp::size_type __n) {
47+
__count_bool(__bit_iterator<_Cp, _IsConst> __first, typename __size_difference_type_traits<_Cp>::size_type __n) {
4848
using _It = __bit_iterator<_Cp, _IsConst>;
4949
using __storage_type = typename _It::__storage_type;
5050
using difference_type = typename _It::difference_type;
@@ -75,8 +75,10 @@ template <class, class _Cp, bool _IsConst, class _Tp, class _Proj, __enable_if_t
7575
_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 __iter_diff_t<__bit_iterator<_Cp, _IsConst> >
7676
__count(__bit_iterator<_Cp, _IsConst> __first, __bit_iterator<_Cp, _IsConst> __last, const _Tp& __value, _Proj&) {
7777
if (__value)
78-
return std::__count_bool<true>(__first, static_cast<typename _Cp::size_type>(__last - __first));
79-
return std::__count_bool<false>(__first, static_cast<typename _Cp::size_type>(__last - __first));
78+
return std::__count_bool<true>(
79+
__first, static_cast<typename __size_difference_type_traits<_Cp>::size_type>(__last - __first));
80+
return std::__count_bool<false>(
81+
__first, static_cast<typename __size_difference_type_traits<_Cp>::size_type>(__last - __first));
8082
}
8183

8284
template <class _InputIterator, class _Tp>

libcxx/include/__algorithm/fill_n.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ __fill_n(_OutputIterator __first, _Size __n, const _Tp& __value);
3232

3333
template <bool _FillVal, class _Cp>
3434
_LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_HIDE_FROM_ABI void
35-
__fill_n_bool(__bit_iterator<_Cp, false> __first, typename _Cp::size_type __n) {
35+
__fill_n_bool(__bit_iterator<_Cp, false> __first, typename __size_difference_type_traits<_Cp>::size_type __n) {
3636
using _It = __bit_iterator<_Cp, false>;
3737
using __storage_type = typename _It::__storage_type;
3838

libcxx/include/__algorithm/find.h

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,7 @@ __find(_Tp* __first, _Tp* __last, const _Up& __value, _Proj& __proj) {
9797
// __bit_iterator implementation
9898
template <bool _ToFind, class _Cp, bool _IsConst>
9999
_LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_HIDE_FROM_ABI __bit_iterator<_Cp, _IsConst>
100-
__find_bool(__bit_iterator<_Cp, _IsConst> __first, typename _Cp::size_type __n) {
100+
__find_bool(__bit_iterator<_Cp, _IsConst> __first, typename __size_difference_type_traits<_Cp>::size_type __n) {
101101
using _It = __bit_iterator<_Cp, _IsConst>;
102102
using __storage_type = typename _It::__storage_type;
103103

@@ -135,8 +135,10 @@ template <class _Cp, bool _IsConst, class _Tp, class _Proj, __enable_if_t<__is_i
135135
inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 __bit_iterator<_Cp, _IsConst>
136136
__find(__bit_iterator<_Cp, _IsConst> __first, __bit_iterator<_Cp, _IsConst> __last, const _Tp& __value, _Proj&) {
137137
if (static_cast<bool>(__value))
138-
return std::__find_bool<true>(__first, static_cast<typename _Cp::size_type>(__last - __first));
139-
return std::__find_bool<false>(__first, static_cast<typename _Cp::size_type>(__last - __first));
138+
return std::__find_bool<true>(
139+
__first, static_cast<typename __size_difference_type_traits<_Cp>::size_type>(__last - __first));
140+
return std::__find_bool<false>(
141+
__first, static_cast<typename __size_difference_type_traits<_Cp>::size_type>(__last - __first));
140142
}
141143

142144
// segmented iterator implementation

libcxx/include/__bit_reference

Lines changed: 20 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -15,13 +15,15 @@
1515
#include <__bit/countr.h>
1616
#include <__compare/ordering.h>
1717
#include <__config>
18+
#include <__cstddef/ptrdiff_t.h>
1819
#include <__cstddef/size_t.h>
1920
#include <__fwd/bit_reference.h>
2021
#include <__iterator/iterator_traits.h>
2122
#include <__memory/construct_at.h>
2223
#include <__memory/pointer_traits.h>
2324
#include <__type_traits/conditional.h>
2425
#include <__type_traits/is_constant_evaluated.h>
26+
#include <__type_traits/void_t.h>
2527
#include <__utility/swap.h>
2628

2729
#if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
@@ -41,6 +43,18 @@ struct __has_storage_type {
4143
static const bool value = false;
4244
};
4345

46+
template <class, class>
47+
struct __size_difference_type_traits {
48+
using difference_type = ptrdiff_t;
49+
using size_type = size_t;
50+
};
51+
52+
template <class _Cp>
53+
struct __size_difference_type_traits<_Cp, __void_t<typename _Cp::difference_type, typename _Cp::size_type> > {
54+
using difference_type = typename _Cp::difference_type;
55+
using size_type = typename _Cp::size_type;
56+
};
57+
4458
template <class _Cp, bool = __has_storage_type<_Cp>::value>
4559
class __bit_reference {
4660
using __storage_type _LIBCPP_NODEBUG = typename _Cp::__storage_type;
@@ -587,7 +601,7 @@ inline _LIBCPP_HIDE_FROM_ABI __bit_iterator<_Cr, false> swap_ranges(
587601

588602
template <class _Cp>
589603
struct __bit_array {
590-
using difference_type _LIBCPP_NODEBUG = typename _Cp::difference_type;
604+
using difference_type _LIBCPP_NODEBUG = typename __size_difference_type_traits<_Cp>::difference_type;
591605
using __storage_type _LIBCPP_NODEBUG = typename _Cp::__storage_type;
592606
using __storage_pointer _LIBCPP_NODEBUG = typename _Cp::__storage_pointer;
593607
using iterator _LIBCPP_NODEBUG = typename _Cp::iterator;
@@ -779,7 +793,7 @@ equal(__bit_iterator<_Cp, _IC1> __first1, __bit_iterator<_Cp, _IC1> __last1, __b
779793
template <class _Cp, bool _IsConst, typename _Cp::__storage_type>
780794
class __bit_iterator {
781795
public:
782-
using difference_type = typename _Cp::difference_type;
796+
using difference_type = typename __size_difference_type_traits<_Cp>::difference_type;
783797
using value_type = bool;
784798
using pointer = __bit_iterator;
785799
#ifndef _LIBCPP_ABI_BITSET_VECTOR_BOOL_CONST_SUBSCRIPT_RETURN_BOOL
@@ -966,7 +980,7 @@ private:
966980

967981
template <bool _FillVal, class _Dp>
968982
_LIBCPP_CONSTEXPR_SINCE_CXX20 friend void
969-
__fill_n_bool(__bit_iterator<_Dp, false> __first, typename _Dp::size_type __n);
983+
__fill_n_bool(__bit_iterator<_Dp, false> __first, typename __size_difference_type_traits<_Dp>::size_type __n);
970984

971985
template <class _Dp, bool _IC>
972986
_LIBCPP_CONSTEXPR_SINCE_CXX20 friend __bit_iterator<_Dp, false> __copy_aligned(
@@ -1009,10 +1023,10 @@ private:
10091023
equal(__bit_iterator<_Dp, _IC1>, __bit_iterator<_Dp, _IC1>, __bit_iterator<_Dp, _IC2>);
10101024
template <bool _ToFind, class _Dp, bool _IC>
10111025
_LIBCPP_CONSTEXPR_SINCE_CXX20 friend __bit_iterator<_Dp, _IC>
1012-
__find_bool(__bit_iterator<_Dp, _IC>, typename _Dp::size_type);
1026+
__find_bool(__bit_iterator<_Dp, _IC>, typename __size_difference_type_traits<_Dp>::size_type);
10131027
template <bool _ToCount, class _Dp, bool _IC>
1014-
friend typename __bit_iterator<_Dp, _IC>::difference_type _LIBCPP_HIDE_FROM_ABI
1015-
_LIBCPP_CONSTEXPR_SINCE_CXX20 __count_bool(__bit_iterator<_Dp, _IC>, typename _Dp::size_type);
1028+
friend typename __bit_iterator<_Dp, _IC>::difference_type _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20
1029+
__count_bool(__bit_iterator<_Dp, _IC>, typename __size_difference_type_traits<_Dp>::size_type);
10161030
};
10171031

10181032
_LIBCPP_END_NAMESPACE_STD

libcxx/include/__fwd/bit_reference.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,9 @@ _LIBCPP_BEGIN_NAMESPACE_STD
2020
template <class _Cp, bool _IsConst, typename _Cp::__storage_type = 0>
2121
class __bit_iterator;
2222

23+
template <class, class = void>
24+
struct __size_difference_type_traits;
25+
2326
_LIBCPP_END_NAMESPACE_STD
2427

2528
#endif // _LIBCPP___FWD_BIT_REFERENCE_H

libcxx/include/bitset

Lines changed: 12 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -136,6 +136,8 @@ template <size_t N> struct hash<std::bitset<N>>;
136136
# include <__assert>
137137
# include <__bit_reference>
138138
# include <__config>
139+
# include <__cstddef/ptrdiff_t.h>
140+
# include <__cstddef/size_t.h>
139141
# include <__functional/hash.h>
140142
# include <__functional/unary_function.h>
141143
# include <__type_traits/is_char_like_type.h>
@@ -170,9 +172,7 @@ struct __has_storage_type<__bitset<_N_words, _Size> > {
170172
template <size_t _N_words, size_t _Size>
171173
class __bitset {
172174
public:
173-
typedef ptrdiff_t difference_type;
174-
typedef size_t size_type;
175-
typedef size_type __storage_type;
175+
typedef size_t __storage_type;
176176

177177
protected:
178178
typedef __bitset __self;
@@ -301,28 +301,28 @@ inline _LIBCPP_CONSTEXPR __bitset<_N_words, _Size>::__bitset(unsigned long long
301301
template <size_t _N_words, size_t _Size>
302302
inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX23 void
303303
__bitset<_N_words, _Size>::operator&=(const __bitset& __v) _NOEXCEPT {
304-
for (size_type __i = 0; __i < _N_words; ++__i)
304+
for (size_t __i = 0; __i < _N_words; ++__i)
305305
__first_[__i] &= __v.__first_[__i];
306306
}
307307

308308
template <size_t _N_words, size_t _Size>
309309
inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX23 void
310310
__bitset<_N_words, _Size>::operator|=(const __bitset& __v) _NOEXCEPT {
311-
for (size_type __i = 0; __i < _N_words; ++__i)
311+
for (size_t __i = 0; __i < _N_words; ++__i)
312312
__first_[__i] |= __v.__first_[__i];
313313
}
314314

315315
template <size_t _N_words, size_t _Size>
316316
inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX23 void
317317
__bitset<_N_words, _Size>::operator^=(const __bitset& __v) _NOEXCEPT {
318-
for (size_type __i = 0; __i < _N_words; ++__i)
318+
for (size_t __i = 0; __i < _N_words; ++__i)
319319
__first_[__i] ^= __v.__first_[__i];
320320
}
321321

322322
template <size_t _N_words, size_t _Size>
323323
_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX23 void __bitset<_N_words, _Size>::flip() _NOEXCEPT {
324324
// do middle whole words
325-
size_type __n = _Size;
325+
size_t __n = _Size;
326326
__storage_pointer __p = __first_;
327327
for (; __n >= __bits_per_word; ++__p, __n -= __bits_per_word)
328328
*__p = ~*__p;
@@ -390,7 +390,7 @@ __bitset<_N_words, _Size>::to_ullong(true_type, true_type) const {
390390
template <size_t _N_words, size_t _Size>
391391
_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX23 bool __bitset<_N_words, _Size>::all() const _NOEXCEPT {
392392
// do middle whole words
393-
size_type __n = _Size;
393+
size_t __n = _Size;
394394
__const_storage_pointer __p = __first_;
395395
for (; __n >= __bits_per_word; ++__p, __n -= __bits_per_word)
396396
if (~*__p)
@@ -407,7 +407,7 @@ _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX23 bool __bitset<_N_words, _Siz
407407
template <size_t _N_words, size_t _Size>
408408
_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX23 bool __bitset<_N_words, _Size>::any() const _NOEXCEPT {
409409
// do middle whole words
410-
size_type __n = _Size;
410+
size_t __n = _Size;
411411
__const_storage_pointer __p = __first_;
412412
for (; __n >= __bits_per_word; ++__p, __n -= __bits_per_word)
413413
if (*__p)
@@ -424,17 +424,15 @@ _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX23 bool __bitset<_N_words, _Siz
424424
template <size_t _N_words, size_t _Size>
425425
inline size_t __bitset<_N_words, _Size>::__hash_code() const _NOEXCEPT {
426426
size_t __h = 0;
427-
for (size_type __i = 0; __i < _N_words; ++__i)
427+
for (size_t __i = 0; __i < _N_words; ++__i)
428428
__h ^= __first_[__i];
429429
return __h;
430430
}
431431

432432
template <size_t _Size>
433433
class __bitset<1, _Size> {
434434
public:
435-
typedef ptrdiff_t difference_type;
436-
typedef size_t size_type;
437-
typedef size_type __storage_type;
435+
typedef size_t __storage_type;
438436

439437
protected:
440438
typedef __bitset __self;
@@ -549,9 +547,7 @@ inline size_t __bitset<1, _Size>::__hash_code() const _NOEXCEPT {
549547
template <>
550548
class __bitset<0, 0> {
551549
public:
552-
typedef ptrdiff_t difference_type;
553-
typedef size_t size_type;
554-
typedef size_type __storage_type;
550+
typedef size_t __storage_type;
555551

556552
protected:
557553
typedef __bitset __self;

libcxx/test/std/utilities/template.bitset/bitset.members/nonstdmem.uglified.compile.pass.cpp

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,8 @@ struct my_base {
2525
typedef const int* const_iterator;
2626
typedef my_base base;
2727
typedef const int& const_reference;
28+
typedef std::ptrdiff_t difference_type;
29+
typedef std::size_t size_type;
2830
};
2931

3032
template <std::size_t N>
@@ -69,3 +71,23 @@ static_assert(std::is_same<my_derived<32>::const_reference, const int&>::value,
6971
static_assert(std::is_same<my_derived<48>::const_reference, const int&>::value, "");
7072
static_assert(std::is_same<my_derived<64>::const_reference, const int&>::value, "");
7173
static_assert(std::is_same<my_derived<96>::const_reference, const int&>::value, "");
74+
75+
static_assert(std::is_same<my_derived<0>::difference_type, std::ptrdiff_t>::value, "");
76+
static_assert(std::is_same<my_derived<1>::difference_type, std::ptrdiff_t>::value, "");
77+
static_assert(std::is_same<my_derived<8>::difference_type, std::ptrdiff_t>::value, "");
78+
static_assert(std::is_same<my_derived<12>::difference_type, std::ptrdiff_t>::value, "");
79+
static_assert(std::is_same<my_derived<16>::difference_type, std::ptrdiff_t>::value, "");
80+
static_assert(std::is_same<my_derived<32>::difference_type, std::ptrdiff_t>::value, "");
81+
static_assert(std::is_same<my_derived<48>::difference_type, std::ptrdiff_t>::value, "");
82+
static_assert(std::is_same<my_derived<64>::difference_type, std::ptrdiff_t>::value, "");
83+
static_assert(std::is_same<my_derived<96>::difference_type, std::ptrdiff_t>::value, "");
84+
85+
static_assert(std::is_same<my_derived<0>::size_type, std::size_t>::value, "");
86+
static_assert(std::is_same<my_derived<1>::size_type, std::size_t>::value, "");
87+
static_assert(std::is_same<my_derived<8>::size_type, std::size_t>::value, "");
88+
static_assert(std::is_same<my_derived<12>::size_type, std::size_t>::value, "");
89+
static_assert(std::is_same<my_derived<16>::size_type, std::size_t>::value, "");
90+
static_assert(std::is_same<my_derived<32>::size_type, std::size_t>::value, "");
91+
static_assert(std::is_same<my_derived<48>::size_type, std::size_t>::value, "");
92+
static_assert(std::is_same<my_derived<64>::size_type, std::size_t>::value, "");
93+
static_assert(std::is_same<my_derived<96>::size_type, std::size_t>::value, "");

0 commit comments

Comments
 (0)