diff --git a/libcxx/include/__algorithm/count.h b/libcxx/include/__algorithm/count.h index 6910b4f43e993..cd9125779ec64 100644 --- a/libcxx/include/__algorithm/count.h +++ b/libcxx/include/__algorithm/count.h @@ -44,7 +44,7 @@ __count(_Iter __first, _Sent __last, const _Tp& __value, _Proj& __proj) { // __bit_iterator implementation template _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 typename __bit_iterator<_Cp, _IsConst>::difference_type -__count_bool(__bit_iterator<_Cp, _IsConst> __first, typename _Cp::size_type __n) { +__count_bool(__bit_iterator<_Cp, _IsConst> __first, typename __size_difference_type_traits<_Cp>::size_type __n) { using _It = __bit_iterator<_Cp, _IsConst>; using __storage_type = typename _It::__storage_type; using difference_type = typename _It::difference_type; @@ -75,8 +75,10 @@ template > __count(__bit_iterator<_Cp, _IsConst> __first, __bit_iterator<_Cp, _IsConst> __last, const _Tp& __value, _Proj&) { if (__value) - return std::__count_bool(__first, static_cast(__last - __first)); - return std::__count_bool(__first, static_cast(__last - __first)); + return std::__count_bool( + __first, static_cast::size_type>(__last - __first)); + return std::__count_bool( + __first, static_cast::size_type>(__last - __first)); } template diff --git a/libcxx/include/__algorithm/fill_n.h b/libcxx/include/__algorithm/fill_n.h index 5069a72783f34..a7e01c45b9222 100644 --- a/libcxx/include/__algorithm/fill_n.h +++ b/libcxx/include/__algorithm/fill_n.h @@ -32,7 +32,7 @@ __fill_n(_OutputIterator __first, _Size __n, const _Tp& __value); template _LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_HIDE_FROM_ABI void -__fill_n_bool(__bit_iterator<_Cp, false> __first, typename _Cp::size_type __n) { +__fill_n_bool(__bit_iterator<_Cp, false> __first, typename __size_difference_type_traits<_Cp>::size_type __n) { using _It = __bit_iterator<_Cp, false>; using __storage_type = typename _It::__storage_type; diff --git a/libcxx/include/__algorithm/find.h b/libcxx/include/__algorithm/find.h index a05d50718595e..24b8b2f96443c 100644 --- a/libcxx/include/__algorithm/find.h +++ b/libcxx/include/__algorithm/find.h @@ -97,7 +97,7 @@ __find(_Tp* __first, _Tp* __last, const _Up& __value, _Proj& __proj) { // __bit_iterator implementation template _LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_HIDE_FROM_ABI __bit_iterator<_Cp, _IsConst> -__find_bool(__bit_iterator<_Cp, _IsConst> __first, typename _Cp::size_type __n) { +__find_bool(__bit_iterator<_Cp, _IsConst> __first, typename __size_difference_type_traits<_Cp>::size_type __n) { using _It = __bit_iterator<_Cp, _IsConst>; using __storage_type = typename _It::__storage_type; @@ -135,8 +135,10 @@ template __find(__bit_iterator<_Cp, _IsConst> __first, __bit_iterator<_Cp, _IsConst> __last, const _Tp& __value, _Proj&) { if (static_cast(__value)) - return std::__find_bool(__first, static_cast(__last - __first)); - return std::__find_bool(__first, static_cast(__last - __first)); + return std::__find_bool( + __first, static_cast::size_type>(__last - __first)); + return std::__find_bool( + __first, static_cast::size_type>(__last - __first)); } // segmented iterator implementation diff --git a/libcxx/include/__bit_reference b/libcxx/include/__bit_reference index 7e27090cc68a4..67abb023122ed 100644 --- a/libcxx/include/__bit_reference +++ b/libcxx/include/__bit_reference @@ -15,6 +15,7 @@ #include <__bit/countr.h> #include <__compare/ordering.h> #include <__config> +#include <__cstddef/ptrdiff_t.h> #include <__cstddef/size_t.h> #include <__fwd/bit_reference.h> #include <__iterator/iterator_traits.h> @@ -22,6 +23,7 @@ #include <__memory/pointer_traits.h> #include <__type_traits/conditional.h> #include <__type_traits/is_constant_evaluated.h> +#include <__type_traits/void_t.h> #include <__utility/swap.h> #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER) @@ -41,6 +43,18 @@ struct __has_storage_type { static const bool value = false; }; +template +struct __size_difference_type_traits { + using difference_type = ptrdiff_t; + using size_type = size_t; +}; + +template +struct __size_difference_type_traits<_Cp, __void_t > { + using difference_type = typename _Cp::difference_type; + using size_type = typename _Cp::size_type; +}; + template ::value> class __bit_reference { using __storage_type _LIBCPP_NODEBUG = typename _Cp::__storage_type; @@ -587,7 +601,7 @@ inline _LIBCPP_HIDE_FROM_ABI __bit_iterator<_Cr, false> swap_ranges( template struct __bit_array { - using difference_type _LIBCPP_NODEBUG = typename _Cp::difference_type; + using difference_type _LIBCPP_NODEBUG = typename __size_difference_type_traits<_Cp>::difference_type; using __storage_type _LIBCPP_NODEBUG = typename _Cp::__storage_type; using __storage_pointer _LIBCPP_NODEBUG = typename _Cp::__storage_pointer; using iterator _LIBCPP_NODEBUG = typename _Cp::iterator; @@ -779,7 +793,7 @@ equal(__bit_iterator<_Cp, _IC1> __first1, __bit_iterator<_Cp, _IC1> __last1, __b template class __bit_iterator { public: - using difference_type = typename _Cp::difference_type; + using difference_type = typename __size_difference_type_traits<_Cp>::difference_type; using value_type = bool; using pointer = __bit_iterator; #ifndef _LIBCPP_ABI_BITSET_VECTOR_BOOL_CONST_SUBSCRIPT_RETURN_BOOL @@ -966,7 +980,7 @@ private: template _LIBCPP_CONSTEXPR_SINCE_CXX20 friend void - __fill_n_bool(__bit_iterator<_Dp, false> __first, typename _Dp::size_type __n); + __fill_n_bool(__bit_iterator<_Dp, false> __first, typename __size_difference_type_traits<_Dp>::size_type __n); template _LIBCPP_CONSTEXPR_SINCE_CXX20 friend __bit_iterator<_Dp, false> __copy_aligned( @@ -1009,10 +1023,10 @@ private: equal(__bit_iterator<_Dp, _IC1>, __bit_iterator<_Dp, _IC1>, __bit_iterator<_Dp, _IC2>); template _LIBCPP_CONSTEXPR_SINCE_CXX20 friend __bit_iterator<_Dp, _IC> - __find_bool(__bit_iterator<_Dp, _IC>, typename _Dp::size_type); + __find_bool(__bit_iterator<_Dp, _IC>, typename __size_difference_type_traits<_Dp>::size_type); template - friend typename __bit_iterator<_Dp, _IC>::difference_type _LIBCPP_HIDE_FROM_ABI - _LIBCPP_CONSTEXPR_SINCE_CXX20 __count_bool(__bit_iterator<_Dp, _IC>, typename _Dp::size_type); + friend typename __bit_iterator<_Dp, _IC>::difference_type _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 + __count_bool(__bit_iterator<_Dp, _IC>, typename __size_difference_type_traits<_Dp>::size_type); }; _LIBCPP_END_NAMESPACE_STD diff --git a/libcxx/include/__fwd/bit_reference.h b/libcxx/include/__fwd/bit_reference.h index 237efb6db6642..30462b6ce4c92 100644 --- a/libcxx/include/__fwd/bit_reference.h +++ b/libcxx/include/__fwd/bit_reference.h @@ -20,6 +20,9 @@ _LIBCPP_BEGIN_NAMESPACE_STD template class __bit_iterator; +template +struct __size_difference_type_traits; + _LIBCPP_END_NAMESPACE_STD #endif // _LIBCPP___FWD_BIT_REFERENCE_H diff --git a/libcxx/include/bitset b/libcxx/include/bitset index c16635dc8092c..10576eb80bf2e 100644 --- a/libcxx/include/bitset +++ b/libcxx/include/bitset @@ -136,6 +136,8 @@ template struct hash>; # include <__assert> # include <__bit_reference> # include <__config> +# include <__cstddef/ptrdiff_t.h> +# include <__cstddef/size_t.h> # include <__functional/hash.h> # include <__functional/unary_function.h> # include <__type_traits/is_char_like_type.h> @@ -170,9 +172,7 @@ struct __has_storage_type<__bitset<_N_words, _Size> > { template class __bitset { public: - typedef ptrdiff_t difference_type; - typedef size_t size_type; - typedef size_type __storage_type; + typedef size_t __storage_type; protected: typedef __bitset __self; @@ -301,28 +301,28 @@ inline _LIBCPP_CONSTEXPR __bitset<_N_words, _Size>::__bitset(unsigned long long template inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX23 void __bitset<_N_words, _Size>::operator&=(const __bitset& __v) _NOEXCEPT { - for (size_type __i = 0; __i < _N_words; ++__i) + for (size_t __i = 0; __i < _N_words; ++__i) __first_[__i] &= __v.__first_[__i]; } template inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX23 void __bitset<_N_words, _Size>::operator|=(const __bitset& __v) _NOEXCEPT { - for (size_type __i = 0; __i < _N_words; ++__i) + for (size_t __i = 0; __i < _N_words; ++__i) __first_[__i] |= __v.__first_[__i]; } template inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX23 void __bitset<_N_words, _Size>::operator^=(const __bitset& __v) _NOEXCEPT { - for (size_type __i = 0; __i < _N_words; ++__i) + for (size_t __i = 0; __i < _N_words; ++__i) __first_[__i] ^= __v.__first_[__i]; } template _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX23 void __bitset<_N_words, _Size>::flip() _NOEXCEPT { // do middle whole words - size_type __n = _Size; + size_t __n = _Size; __storage_pointer __p = __first_; for (; __n >= __bits_per_word; ++__p, __n -= __bits_per_word) *__p = ~*__p; @@ -390,7 +390,7 @@ __bitset<_N_words, _Size>::to_ullong(true_type, true_type) const { template _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX23 bool __bitset<_N_words, _Size>::all() const _NOEXCEPT { // do middle whole words - size_type __n = _Size; + size_t __n = _Size; __const_storage_pointer __p = __first_; for (; __n >= __bits_per_word; ++__p, __n -= __bits_per_word) if (~*__p) @@ -407,7 +407,7 @@ _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX23 bool __bitset<_N_words, _Siz template _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX23 bool __bitset<_N_words, _Size>::any() const _NOEXCEPT { // do middle whole words - size_type __n = _Size; + size_t __n = _Size; __const_storage_pointer __p = __first_; for (; __n >= __bits_per_word; ++__p, __n -= __bits_per_word) if (*__p) @@ -424,7 +424,7 @@ _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX23 bool __bitset<_N_words, _Siz template inline size_t __bitset<_N_words, _Size>::__hash_code() const _NOEXCEPT { size_t __h = 0; - for (size_type __i = 0; __i < _N_words; ++__i) + for (size_t __i = 0; __i < _N_words; ++__i) __h ^= __first_[__i]; return __h; } @@ -432,9 +432,7 @@ inline size_t __bitset<_N_words, _Size>::__hash_code() const _NOEXCEPT { template class __bitset<1, _Size> { public: - typedef ptrdiff_t difference_type; - typedef size_t size_type; - typedef size_type __storage_type; + typedef size_t __storage_type; protected: typedef __bitset __self; @@ -549,9 +547,7 @@ inline size_t __bitset<1, _Size>::__hash_code() const _NOEXCEPT { template <> class __bitset<0, 0> { public: - typedef ptrdiff_t difference_type; - typedef size_t size_type; - typedef size_type __storage_type; + typedef size_t __storage_type; protected: typedef __bitset __self; diff --git a/libcxx/test/std/utilities/template.bitset/bitset.members/nonstdmem.uglified.compile.pass.cpp b/libcxx/test/std/utilities/template.bitset/bitset.members/nonstdmem.uglified.compile.pass.cpp index ee5c64f9df5c7..582eb31647ae2 100644 --- a/libcxx/test/std/utilities/template.bitset/bitset.members/nonstdmem.uglified.compile.pass.cpp +++ b/libcxx/test/std/utilities/template.bitset/bitset.members/nonstdmem.uglified.compile.pass.cpp @@ -25,6 +25,8 @@ struct my_base { typedef const int* const_iterator; typedef my_base base; typedef const int& const_reference; + typedef std::ptrdiff_t difference_type; + typedef std::size_t size_type; }; template @@ -69,3 +71,23 @@ static_assert(std::is_same::const_reference, const int&>::value, static_assert(std::is_same::const_reference, const int&>::value, ""); static_assert(std::is_same::const_reference, const int&>::value, ""); static_assert(std::is_same::const_reference, const int&>::value, ""); + +static_assert(std::is_same::difference_type, std::ptrdiff_t>::value, ""); +static_assert(std::is_same::difference_type, std::ptrdiff_t>::value, ""); +static_assert(std::is_same::difference_type, std::ptrdiff_t>::value, ""); +static_assert(std::is_same::difference_type, std::ptrdiff_t>::value, ""); +static_assert(std::is_same::difference_type, std::ptrdiff_t>::value, ""); +static_assert(std::is_same::difference_type, std::ptrdiff_t>::value, ""); +static_assert(std::is_same::difference_type, std::ptrdiff_t>::value, ""); +static_assert(std::is_same::difference_type, std::ptrdiff_t>::value, ""); +static_assert(std::is_same::difference_type, std::ptrdiff_t>::value, ""); + +static_assert(std::is_same::size_type, std::size_t>::value, ""); +static_assert(std::is_same::size_type, std::size_t>::value, ""); +static_assert(std::is_same::size_type, std::size_t>::value, ""); +static_assert(std::is_same::size_type, std::size_t>::value, ""); +static_assert(std::is_same::size_type, std::size_t>::value, ""); +static_assert(std::is_same::size_type, std::size_t>::value, ""); +static_assert(std::is_same::size_type, std::size_t>::value, ""); +static_assert(std::is_same::size_type, std::size_t>::value, ""); +static_assert(std::is_same::size_type, std::size_t>::value, "");