[libcxx-commits] [libcxx] 87abc50 - [libc++][NFC] Inline the string constructors

Nikolas Klauser via libcxx-commits libcxx-commits at lists.llvm.org
Sat Sep 17 09:00:20 PDT 2022


Author: Nikolas Klauser
Date: 2022-09-17T18:00:11+02:00
New Revision: 87abc5013ac05a8b7ef86ee98ffcabdd8908802c

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

LOG: [libc++][NFC] Inline the string constructors

This removes a lot of boilerplate code.

Reviewed By: ldionne, #libc

Spies: EricWF, libcxx-commits

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

Added: 
    

Modified: 
    libcxx/include/string

Removed: 
    


################################################################################
diff  --git a/libcxx/include/string b/libcxx/include/string
index 44c2045e37e5f..4e9bf64b5a3f8 100644
--- a/libcxx/include/string
+++ b/libcxx/include/string
@@ -816,94 +816,156 @@ private:
     }
 
 public:
-    _LIBCPP_TEMPLATE_DATA_VIS
-    static const size_type npos = -1;
+  _LIBCPP_TEMPLATE_DATA_VIS static const size_type npos = -1;
 
-    _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 basic_string()
-        _NOEXCEPT_(is_nothrow_default_constructible<allocator_type>::value);
+  _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 basic_string()
+      _NOEXCEPT_(is_nothrow_default_constructible<allocator_type>::value)
+      : __r_(__default_init_tag(), __default_init_tag()) {
+    std::__debug_db_insert_c(this);
+    __default_init();
+  }
 
-    _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 explicit basic_string(const allocator_type& __a)
+  _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 explicit basic_string(const allocator_type& __a)
 #if _LIBCPP_STD_VER <= 14
-        _NOEXCEPT_(is_nothrow_copy_constructible<allocator_type>::value);
+      _NOEXCEPT_(is_nothrow_copy_constructible<allocator_type>::value)
 #else
-        _NOEXCEPT;
+      _NOEXCEPT
 #endif
+      : __r_(__default_init_tag(), __a) {
+    std::__debug_db_insert_c(this);
+    __default_init();
+  }
 
-    _LIBCPP_CONSTEXPR_SINCE_CXX20 basic_string(const basic_string& __str);
-    _LIBCPP_CONSTEXPR_SINCE_CXX20 basic_string(const basic_string& __str, const allocator_type& __a);
+  _LIBCPP_CONSTEXPR_SINCE_CXX20 basic_string(const basic_string& __str);
+  _LIBCPP_CONSTEXPR_SINCE_CXX20 basic_string(const basic_string& __str, const allocator_type& __a);
 
 #ifndef _LIBCPP_CXX03_LANG
-    _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20
-    basic_string(basic_string&& __str)
-#if _LIBCPP_STD_VER <= 14
-        _NOEXCEPT_(is_nothrow_move_constructible<allocator_type>::value);
-#else
-        _NOEXCEPT;
-#endif
+  _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 basic_string(basic_string&& __str)
+#  if _LIBCPP_STD_VER <= 14
+      _NOEXCEPT_(is_nothrow_move_constructible<allocator_type>::value)
+#  else
+      _NOEXCEPT
+#  endif
+      : __r_(std::move(__str.__r_)) {
+    __str.__default_init();
+    std::__debug_db_insert_c(this);
+    if (__is_long())
+      std::__debug_db_swap(this, &__str);
+  }
 
-    _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20
-    basic_string(basic_string&& __str, const allocator_type& __a);
+  _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 basic_string(basic_string&& __str, const allocator_type& __a)
+      : __r_(__default_init_tag(), __a) {
+    if (__str.__is_long() && __a != __str.__alloc()) // copy, not move
+      __init(std::__to_address(__str.__get_long_pointer()), __str.__get_long_size());
+    else {
+      if (__libcpp_is_constant_evaluated())
+        __r_.first() = __rep();
+      __r_.first() = __str.__r_.first();
+      __str.__default_init();
+    }
+    std::__debug_db_insert_c(this);
+    if (__is_long())
+      std::__debug_db_swap(this, &__str);
+  }
 #endif // _LIBCPP_CXX03_LANG
 
-    template <class = __enable_if_t<__is_allocator<_Allocator>::value, nullptr_t> >
-    _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20
-    basic_string(const _CharT* __s) : __r_(__default_init_tag(), __default_init_tag()) {
-      _LIBCPP_ASSERT(__s != nullptr, "basic_string(const char*) detected nullptr");
-      __init(__s, traits_type::length(__s));
-      std::__debug_db_insert_c(this);
-    }
+  template <class = __enable_if_t<__is_allocator<_Allocator>::value, nullptr_t> >
+  _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 basic_string(const _CharT* __s)
+      : __r_(__default_init_tag(), __default_init_tag()) {
+    _LIBCPP_ASSERT(__s != nullptr, "basic_string(const char*) detected nullptr");
+    __init(__s, traits_type::length(__s));
+    std::__debug_db_insert_c(this);
+  }
 
-    template <class = __enable_if_t<__is_allocator<_Allocator>::value, nullptr_t> >
-        _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20
-        basic_string(const _CharT* __s, const _Allocator& __a);
+  template <class = __enable_if_t<__is_allocator<_Allocator>::value, nullptr_t> >
+  _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 basic_string(const _CharT* __s, const _Allocator& __a);
 
 #if _LIBCPP_STD_VER > 20
-    basic_string(nullptr_t) = delete;
+  basic_string(nullptr_t) = delete;
 #endif
 
-    _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20
-    basic_string(const _CharT* __s, size_type __n);
-    _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20
-    basic_string(const _CharT* __s, size_type __n, const _Allocator& __a);
-    _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20
-    basic_string(size_type __n, _CharT __c);
+  _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 basic_string(const _CharT* __s, size_type __n)
+      : __r_(__default_init_tag(), __default_init_tag()) {
+    _LIBCPP_ASSERT(__n == 0 || __s != nullptr, "basic_string(const char*, n) detected nullptr");
+    __init(__s, __n);
+    std::__debug_db_insert_c(this);
+  }
 
-    template <class = __enable_if_t<__is_allocator<_Allocator>::value, nullptr_t> >
-        _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20
-        basic_string(size_type __n, _CharT __c, const _Allocator& __a);
+  _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20
+  basic_string(const _CharT* __s, size_type __n, const _Allocator& __a)
+      : __r_(__default_init_tag(), __a) {
+    _LIBCPP_ASSERT(__n == 0 || __s != nullptr, "basic_string(const char*, n, allocator) detected nullptr");
+    __init(__s, __n);
+    std::__debug_db_insert_c(this);
+  }
 
-    _LIBCPP_CONSTEXPR_SINCE_CXX20
-    basic_string(const basic_string& __str, size_type __pos, size_type __n,
-                 const _Allocator& __a = _Allocator());
-    _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20
-    basic_string(const basic_string& __str, size_type __pos,
-                 const _Allocator& __a = _Allocator());
+  _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 basic_string(size_type __n, _CharT __c)
+      : __r_(__default_init_tag(), __default_init_tag()) {
+    __init(__n, __c);
+    std::__debug_db_insert_c(this);
+  }
 
-    template<class _Tp, class = __enable_if_t<__can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value && !__is_same_uncvref<_Tp, basic_string>::value> >
-        _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS _LIBCPP_CONSTEXPR_SINCE_CXX20
-        basic_string(const _Tp& __t, size_type __pos, size_type __n,
-                     const allocator_type& __a = allocator_type());
+  template <class = __enable_if_t<__is_allocator<_Allocator>::value, nullptr_t> >
+  _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 basic_string(size_type __n, _CharT __c, const _Allocator& __a);
 
-    template<class _Tp, class = __enable_if_t<__can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value &&
-                                          !__is_same_uncvref<_Tp, basic_string>::value> >
-        _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS _LIBCPP_CONSTEXPR_SINCE_CXX20
-        explicit basic_string(const _Tp& __t);
+  _LIBCPP_CONSTEXPR_SINCE_CXX20
+  basic_string(const basic_string& __str, size_type __pos, size_type __n, const _Allocator& __a = _Allocator());
 
-    template<class _Tp, class = __enable_if_t<__can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value && !__is_same_uncvref<_Tp, basic_string>::value> >
-        _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS _LIBCPP_CONSTEXPR_SINCE_CXX20
-        explicit basic_string(const _Tp& __t, const allocator_type& __a);
+  _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20
+  basic_string(const basic_string& __str, size_type __pos, const _Allocator& __a = _Allocator())
+      : __r_(__default_init_tag(), __a) {
+    size_type __str_sz = __str.size();
+    if (__pos > __str_sz)
+      __throw_out_of_range();
+    __init(__str.data() + __pos, __str_sz - __pos);
+    std::__debug_db_insert_c(this);
+  }
+
+  template <class _Tp,
+            class = __enable_if_t<__can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value &&
+                                  !__is_same_uncvref<_Tp, basic_string>::value> >
+  _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS _LIBCPP_CONSTEXPR_SINCE_CXX20
+  basic_string(const _Tp& __t, size_type __pos, size_type __n, const allocator_type& __a = allocator_type());
+
+  template <class _Tp,
+            class = __enable_if_t<__can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value &&
+                                  !__is_same_uncvref<_Tp, basic_string>::value> >
+  _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS _LIBCPP_CONSTEXPR_SINCE_CXX20 explicit basic_string(
+      const _Tp& __t);
+
+  template <class _Tp,
+            class = __enable_if_t<__can_be_converted_to_string_view<_CharT, _Traits, _Tp>::value &&
+                                  !__is_same_uncvref<_Tp, basic_string>::value> >
+  _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS _LIBCPP_CONSTEXPR_SINCE_CXX20 explicit basic_string(
+      const _Tp& __t, const allocator_type& __a);
+
+  template <class _InputIterator, class = __enable_if_t<__is_cpp17_input_iterator<_InputIterator>::value> >
+  _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 basic_string(_InputIterator __first, _InputIterator __last)
+      : __r_(__default_init_tag(), __default_init_tag()) {
+    __init(__first, __last);
+    std::__debug_db_insert_c(this);
+  }
+
+  template <class _InputIterator, class = __enable_if_t<__is_cpp17_input_iterator<_InputIterator>::value> >
+  _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20
+  basic_string(_InputIterator __first, _InputIterator __last, const allocator_type& __a)
+      : __r_(__default_init_tag(), __a) {
+    __init(__first, __last);
+    std::__debug_db_insert_c(this);
+  }
 
-    template<class _InputIterator, class = __enable_if_t<__is_cpp17_input_iterator<_InputIterator>::value> >
-        _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20
-        basic_string(_InputIterator __first, _InputIterator __last);
-    template<class _InputIterator, class = __enable_if_t<__is_cpp17_input_iterator<_InputIterator>::value> >
-        _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20
-        basic_string(_InputIterator __first, _InputIterator __last, const allocator_type& __a);
 #ifndef _LIBCPP_CXX03_LANG
-    _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20
-    basic_string(initializer_list<_CharT> __il);
-    _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20
-    basic_string(initializer_list<_CharT> __il, const _Allocator& __a);
+  _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 basic_string(initializer_list<_CharT> __il)
+      : __r_(__default_init_tag(), __default_init_tag()) {
+    __init(__il.begin(), __il.end());
+    std::__debug_db_insert_c(this);
+  }
+
+  _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 basic_string(initializer_list<_CharT> __il, const _Allocator& __a)
+      : __r_(__default_init_tag(), __a) {
+    __init(__il.begin(), __il.end());
+    std::__debug_db_insert_c(this);
+  }
 #endif // _LIBCPP_CXX03_LANG
 
     inline _LIBCPP_CONSTEXPR_SINCE_CXX20 ~basic_string();
@@ -1896,30 +1958,6 @@ basic_string<_CharT, _Traits, _Allocator>::__invalidate_iterators_past(size_type
 #endif // _LIBCPP_ENABLE_DEBUG_MODE
 }
 
-template <class _CharT, class _Traits, class _Allocator>
-inline _LIBCPP_CONSTEXPR_SINCE_CXX20
-basic_string<_CharT, _Traits, _Allocator>::basic_string()
-    _NOEXCEPT_(is_nothrow_default_constructible<allocator_type>::value)
-     : __r_(__default_init_tag(), __default_init_tag())
-{
-    std::__debug_db_insert_c(this);
-    __default_init();
-}
-
-template <class _CharT, class _Traits, class _Allocator>
-inline _LIBCPP_CONSTEXPR_SINCE_CXX20
-basic_string<_CharT, _Traits, _Allocator>::basic_string(const allocator_type& __a)
-#if _LIBCPP_STD_VER <= 14
-        _NOEXCEPT_(is_nothrow_copy_constructible<allocator_type>::value)
-#else
-        _NOEXCEPT
-#endif
-: __r_(__default_init_tag(), __a)
-{
-    std::__debug_db_insert_c(this);
-    __default_init();
-}
-
 template <class _CharT, class _Traits, class _Allocator>
 _LIBCPP_CONSTEXPR_SINCE_CXX20
 void basic_string<_CharT, _Traits, _Allocator>::__init(const value_type* __s,
@@ -1988,26 +2026,6 @@ basic_string<_CharT, _Traits, _Allocator>::basic_string(const _CharT* __s, const
     std::__debug_db_insert_c(this);
 }
 
-template <class _CharT, class _Traits, class _Allocator>
-inline _LIBCPP_CONSTEXPR_SINCE_CXX20
-basic_string<_CharT, _Traits, _Allocator>::basic_string(const _CharT* __s, size_type __n)
-     : __r_(__default_init_tag(), __default_init_tag())
-{
-    _LIBCPP_ASSERT(__n == 0 || __s != nullptr, "basic_string(const char*, n) detected nullptr");
-    __init(__s, __n);
-    std::__debug_db_insert_c(this);
-}
-
-template <class _CharT, class _Traits, class _Allocator>
-inline _LIBCPP_CONSTEXPR_SINCE_CXX20
-basic_string<_CharT, _Traits, _Allocator>::basic_string(const _CharT* __s, size_type __n, const _Allocator& __a)
-    : __r_(__default_init_tag(), __a)
-{
-    _LIBCPP_ASSERT(__n == 0 || __s != nullptr, "basic_string(const char*, n, allocator) detected nullptr");
-    __init(__s, __n);
-    std::__debug_db_insert_c(this);
-}
-
 template <class _CharT, class _Traits, class _Allocator>
 _LIBCPP_CONSTEXPR_SINCE_CXX20
 basic_string<_CharT, _Traits, _Allocator>::basic_string(const basic_string& __str)
@@ -2059,45 +2077,6 @@ void basic_string<_CharT, _Traits, _Allocator>::__init_copy_ctor_external(
   traits_type::copy(std::__to_address(__p), __s, __sz + 1);
 }
 
-#ifndef _LIBCPP_CXX03_LANG
-
-template <class _CharT, class _Traits, class _Allocator>
-inline _LIBCPP_CONSTEXPR_SINCE_CXX20
-basic_string<_CharT, _Traits, _Allocator>::basic_string(basic_string&& __str)
-#if _LIBCPP_STD_VER <= 14
-        _NOEXCEPT_(is_nothrow_move_constructible<allocator_type>::value)
-#else
-        _NOEXCEPT
-#endif
-    : __r_(std::move(__str.__r_))
-{
-    __str.__default_init();
-    std::__debug_db_insert_c(this);
-    if (__is_long())
-        std::__debug_db_swap(this, &__str);
-}
-
-template <class _CharT, class _Traits, class _Allocator>
-inline _LIBCPP_CONSTEXPR_SINCE_CXX20
-basic_string<_CharT, _Traits, _Allocator>::basic_string(basic_string&& __str, const allocator_type& __a)
-    : __r_(__default_init_tag(), __a)
-{
-    if (__str.__is_long() && __a != __str.__alloc()) // copy, not move
-        __init(std::__to_address(__str.__get_long_pointer()), __str.__get_long_size());
-    else
-    {
-        if (__libcpp_is_constant_evaluated())
-            __r_.first() = __rep();
-        __r_.first() = __str.__r_.first();
-        __str.__default_init();
-    }
-    std::__debug_db_insert_c(this);
-    if (__is_long())
-        std::__debug_db_swap(this, &__str);
-}
-
-#endif // _LIBCPP_CXX03_LANG
-
 template <class _CharT, class _Traits, class _Allocator>
 _LIBCPP_CONSTEXPR_SINCE_CXX20
 void
@@ -2127,15 +2106,6 @@ basic_string<_CharT, _Traits, _Allocator>::__init(size_type __n, value_type __c)
     traits_type::assign(__p[__n], value_type());
 }
 
-template <class _CharT, class _Traits, class _Allocator>
-inline _LIBCPP_CONSTEXPR_SINCE_CXX20
-basic_string<_CharT, _Traits, _Allocator>::basic_string(size_type __n, _CharT __c)
-     : __r_(__default_init_tag(), __default_init_tag())
-{
-    __init(__n, __c);
-    std::__debug_db_insert_c(this);
-}
-
 template <class _CharT, class _Traits, class _Allocator>
 template <class>
 _LIBCPP_CONSTEXPR_SINCE_CXX20
@@ -2160,19 +2130,6 @@ basic_string<_CharT, _Traits, _Allocator>::basic_string(const basic_string& __st
     std::__debug_db_insert_c(this);
 }
 
-template <class _CharT, class _Traits, class _Allocator>
-inline _LIBCPP_CONSTEXPR_SINCE_CXX20
-basic_string<_CharT, _Traits, _Allocator>::basic_string(const basic_string& __str, size_type __pos,
-                                                        const _Allocator& __a)
-    : __r_(__default_init_tag(), __a)
-{
-    size_type __str_sz = __str.size();
-    if (__pos > __str_sz)
-        __throw_out_of_range();
-    __init(__str.data() + __pos, __str_sz - __pos);
-    std::__debug_db_insert_c(this);
-}
-
 template <class _CharT, class _Traits, class _Allocator>
 template <class _Tp, class>
 _LIBCPP_CONSTEXPR_SINCE_CXX20
@@ -2283,51 +2240,6 @@ basic_string<_CharT, _Traits, _Allocator>::__init(_ForwardIterator __first, _For
 #endif  // _LIBCPP_NO_EXCEPTIONS
 }
 
-template <class _CharT, class _Traits, class _Allocator>
-template<class _InputIterator, class>
-inline _LIBCPP_CONSTEXPR_SINCE_CXX20
-basic_string<_CharT, _Traits, _Allocator>::basic_string(_InputIterator __first, _InputIterator __last)
-     : __r_(__default_init_tag(), __default_init_tag())
-{
-    __init(__first, __last);
-    std::__debug_db_insert_c(this);
-}
-
-template <class _CharT, class _Traits, class _Allocator>
-template<class _InputIterator, class>
-inline _LIBCPP_CONSTEXPR_SINCE_CXX20
-basic_string<_CharT, _Traits, _Allocator>::basic_string(_InputIterator __first, _InputIterator __last,
-                                                        const allocator_type& __a)
-    : __r_(__default_init_tag(), __a)
-{
-    __init(__first, __last);
-    std::__debug_db_insert_c(this);
-}
-
-#ifndef _LIBCPP_CXX03_LANG
-
-template <class _CharT, class _Traits, class _Allocator>
-inline _LIBCPP_CONSTEXPR_SINCE_CXX20
-basic_string<_CharT, _Traits, _Allocator>::basic_string(
-    initializer_list<_CharT> __il)
-     : __r_(__default_init_tag(), __default_init_tag())
-{
-    __init(__il.begin(), __il.end());
-    std::__debug_db_insert_c(this);
-}
-
-template <class _CharT, class _Traits, class _Allocator>
-inline _LIBCPP_CONSTEXPR_SINCE_CXX20
-basic_string<_CharT, _Traits, _Allocator>::basic_string(
-    initializer_list<_CharT> __il, const _Allocator& __a)
-    : __r_(__default_init_tag(), __a)
-{
-    __init(__il.begin(), __il.end());
-    std::__debug_db_insert_c(this);
-}
-
-#endif // _LIBCPP_CXX03_LANG
-
 template <class _CharT, class _Traits, class _Allocator>
 _LIBCPP_CONSTEXPR_SINCE_CXX20
 basic_string<_CharT, _Traits, _Allocator>::~basic_string()


        


More information about the libcxx-commits mailing list