[libcxx-commits] [libcxx] c273f5e - [libc++][nfc] remove duplicated __to_unsigned.

Mark de Wever via libcxx-commits libcxx-commits at lists.llvm.org
Wed May 12 12:09:55 PDT 2021


Author: Mark de Wever
Date: 2021-05-12T21:09:49+02:00
New Revision: c273f5ef7d3f5ac05f67ec899e25830cd9543e56

URL: https://github.com/llvm/llvm-project/commit/c273f5ef7d3f5ac05f67ec899e25830cd9543e56
DIFF: https://github.com/llvm/llvm-project/commit/c273f5ef7d3f5ac05f67ec899e25830cd9543e56.diff

LOG: [libc++][nfc] remove duplicated __to_unsigned.

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

Added: 
    

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

Removed: 
    


################################################################################
diff  --git a/libcxx/include/__ranges/size.h b/libcxx/include/__ranges/size.h
index f2cfecfad3fce..555b14e0eecad 100644
--- a/libcxx/include/__ranges/size.h
+++ b/libcxx/include/__ranges/size.h
@@ -89,7 +89,7 @@ namespace __size {
     template<__
diff erence _Tp>
     [[nodiscard]] constexpr __integer_like auto operator()(_Tp&& __t) const
         noexcept(noexcept(ranges::end(__t) - ranges::begin(__t))) {
-      return __to_unsigned_like<range_
diff erence_t<remove_cvref_t<_Tp>>>(
+      return _VSTD::__to_unsigned_like<range_
diff erence_t<remove_cvref_t<_Tp>>>(
           ranges::end(__t) - ranges::begin(__t));
     }
   };

diff  --git a/libcxx/include/charconv b/libcxx/include/charconv
index 64a2d03520205..5077e393d1030 100644
--- a/libcxx/include/charconv
+++ b/libcxx/include/charconv
@@ -331,19 +331,12 @@ __complement(_Tp __x)
     return _Tp(~__x + 1);
 }
 
-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);
 }

diff  --git a/libcxx/include/type_traits b/libcxx/include/type_traits
index 8751ff162c995..3d0ac45ccaf85 100644
--- a/libcxx/include/type_traits
+++ b/libcxx/include/type_traits
@@ -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
 


        


More information about the libcxx-commits mailing list