[libc++][nfc] remove duplicated __to_unsigned.
authorMark de Wever <koraq@xs4all.nl>
Wed, 12 May 2021 15:46:24 +0000 (17:46 +0200)
committerMark de Wever <koraq@xs4all.nl>
Wed, 12 May 2021 19:09:49 +0000 (21:09 +0200)
Both `<type_traits>` and `<charconv>` implemented this function with
different names and a slightly different behavior. This removes the
version in `<charconv>` and improves the version in `<typetraits>`.

- The code can be used again in C++11.
-  The original claimed C++14 support, but `[[nodiscard]]` is not
   available in  C++14.
- Adds `_LIBCPP_INLINE_VISIBILITY`.

Reviewed By: zoecarver, #libc, Quuxplusone

Differential Revision: https://reviews.llvm.org/D102332

libcxx/include/__ranges/size.h
libcxx/include/charconv
libcxx/include/type_traits

index f2cfecf..555b14e 100644 (file)
@@ -89,7 +89,7 @@ namespace __size {
     template<__difference _Tp>
     [[nodiscard]] constexpr __integer_like auto operator()(_Tp&& __t) const
         noexcept(noexcept(ranges::end(__t) - ranges::begin(__t))) {
-      return __to_unsigned_like<range_difference_t<remove_cvref_t<_Tp>>>(
+      return _VSTD::__to_unsigned_like<range_difference_t<remove_cvref_t<_Tp>>>(
           ranges::end(__t) - ranges::begin(__t));
     }
   };
index 64a2d03..5077e39 100644 (file)
@@ -332,18 +332,11 @@ __complement(_Tp __x)
 }
 
 template <typename _Tp>
-inline _LIBCPP_INLINE_VISIBILITY typename make_unsigned<_Tp>::type
-__to_unsigned(_Tp __x)
-{
-    return static_cast<typename make_unsigned<_Tp>::type>(__x);
-}
-
-template <typename _Tp>
 _LIBCPP_AVAILABILITY_TO_CHARS
 inline _LIBCPP_INLINE_VISIBILITY to_chars_result
 __to_chars_itoa(char* __first, char* __last, _Tp __value, true_type)
 {
-    auto __x = __to_unsigned(__value);
+    auto __x = __to_unsigned_like(__value);
     if (__value < 0 && __first != __last)
     {
         *__first++ = '-';
@@ -391,7 +384,7 @@ inline _LIBCPP_INLINE_VISIBILITY to_chars_result
 __to_chars_integral(char* __first, char* __last, _Tp __value, int __base,
                     true_type)
 {
-    auto __x = __to_unsigned(__value);
+    auto __x = __to_unsigned_like(__value);
     if (__value < 0 && __first != __last)
     {
         *__first++ = '-';
@@ -474,7 +467,7 @@ inline _LIBCPP_INLINE_VISIBILITY from_chars_result
 __sign_combinator(_It __first, _It __last, _Tp& __value, _Fn __f, _Ts... __args)
 {
     using __tl = numeric_limits<_Tp>;
-    decltype(__to_unsigned(__value)) __x;
+    decltype(__to_unsigned_like(__value)) __x;
 
     bool __neg = (__first != __last && *__first == '-');
     auto __r = __f(__neg ? __first + 1 : __first, __last, __x, __args...);
@@ -490,7 +483,7 @@ __sign_combinator(_It __first, _It __last, _Tp& __value, _Fn __f, _Ts... __args)
 
     if (__neg)
     {
-        if (__x <= __complement(__to_unsigned(__tl::min())))
+        if (__x <= __complement(__to_unsigned_like(__tl::min())))
         {
             __x = __complement(__x);
             _VSTD::memcpy(&__value, &__x, sizeof(__x));
@@ -605,7 +598,7 @@ template <typename _Tp, typename enable_if<is_signed<_Tp>::value, int>::type = 0
 inline _LIBCPP_INLINE_VISIBILITY from_chars_result
 __from_chars_atoi(const char* __first, const char* __last, _Tp& __value)
 {
-    using __t = decltype(__to_unsigned(__value));
+    using __t = decltype(__to_unsigned_like(__value));
     return __sign_combinator(__first, __last, __value, __from_chars_atoi<__t>);
 }
 
@@ -661,7 +654,7 @@ inline _LIBCPP_INLINE_VISIBILITY from_chars_result
 __from_chars_integral(const char* __first, const char* __last, _Tp& __value,
                       int __base)
 {
-    using __t = decltype(__to_unsigned(__value));
+    using __t = decltype(__to_unsigned_like(__value));
     return __sign_combinator(__first, __last, __value,
                              __from_chars_integral<__t>, __base);
 }
index 8751ff1..3d0ac45 100644 (file)
@@ -2309,10 +2309,13 @@ struct _LIBCPP_TEMPLATE_VIS make_unsigned
 
 #if _LIBCPP_STD_VER > 11
 template <class _Tp> using make_unsigned_t = typename make_unsigned<_Tp>::type;
+#endif
 
-template<class _From>
-[[nodiscard]] constexpr auto __to_unsigned_like(_From __x) noexcept {
-  return static_cast<make_unsigned_t<_From>>(__x);
+#ifndef _LIBCPP_CXX03_LANG
+template <class _Tp>
+_LIBCPP_NODISCARD_ATTRIBUTE _LIBCPP_INLINE_VISIBILITY constexpr
+typename make_unsigned<_Tp>::type __to_unsigned_like(_Tp __x) noexcept {
+    return static_cast<typename make_unsigned<_Tp>::type>(__x);
 }
 #endif