From 252c9967ba785aedf3b39e2cd50237d0f32fe3bd Mon Sep 17 00:00:00 2001 From: Jonathan Wakely Date: Wed, 14 Oct 2020 12:10:26 +0100 Subject: [PATCH] libstdc++: Define some std::string constructors inline There are a lot of very simple constructors for the old string which are not defined inline. I don't see any reason for this and it probably makes them less likely to be optimized away. Move the definitions into the class body. libstdc++-v3/ChangeLog: * include/bits/basic_string.h (basic_string(const Alloc&)) (basic_string(const basic_string&) (basic_string(const CharT*, size_type, const Alloc&)) (basic_string(const CharT*, const Alloc&)) (basic_string(size_type, CharT, const Alloc&)) (basic_string(initializer_list, const Alloc&)) (basic_string(InputIterator, InputIterator, const Alloc&)): Define inline in class body. * include/bits/basic_string.tcc (basic_string(const Alloc&)) (basic_string(const basic_string&) (basic_string(const CharT*, size_type, const Alloc&)) (basic_string(const CharT*, const Alloc&)) (basic_string(size_type, CharT, const Alloc&)) (basic_string(initializer_list, const Alloc&)) (basic_string(InputIterator, InputIterator, const Alloc&)): Move definitions into class body. --- libstdc++-v3/include/bits/basic_string.h | 39 ++++++++++++----- libstdc++-v3/include/bits/basic_string.tcc | 51 ---------------------- 2 files changed, 28 insertions(+), 62 deletions(-) diff --git a/libstdc++-v3/include/bits/basic_string.h b/libstdc++-v3/include/bits/basic_string.h index 4b3722bdbf1..372302ba6a1 100644 --- a/libstdc++-v3/include/bits/basic_string.h +++ b/libstdc++-v3/include/bits/basic_string.h @@ -548,7 +548,7 @@ _GLIBCXX_BEGIN_NAMESPACE_CXX11 * * The newly-created string contains the exact contents of @a __str. * @a __str is a valid, but unspecified string. - **/ + */ basic_string(basic_string&& __str) noexcept : _M_dataplus(_M_local_data(), std::move(__str._M_get_allocator())) { @@ -696,7 +696,7 @@ _GLIBCXX_BEGIN_NAMESPACE_CXX11 * * The contents of @a str are moved into this string (without copying). * @a str is a valid, but unspecified string. - **/ + */ // _GLIBCXX_RESOLVE_LIB_DEFECTS // 2063. Contradictory requirements for string move assignment basic_string& @@ -3563,14 +3563,20 @@ _GLIBCXX_END_NAMESPACE_CXX11 * @brief Construct an empty string using allocator @a a. */ explicit - basic_string(const _Alloc& __a); + basic_string(const _Alloc& __a) + : _M_dataplus(_S_construct(size_type(), _CharT(), __a), __a) + { } // NB: per LWG issue 42, semantics different from IS: /** * @brief Construct string with copy of value of @a str. * @param __str Source string. */ - basic_string(const basic_string& __str); + basic_string(const basic_string& __str) + : _M_dataplus(__str._M_rep()->_M_grab(_Alloc(__str.get_allocator()), + __str.get_allocator()), + __str.get_allocator()) + { } // _GLIBCXX_RESOLVE_LIB_DEFECTS // 2583. no way to supply an allocator for basic_string(str, pos) @@ -3611,7 +3617,9 @@ _GLIBCXX_END_NAMESPACE_CXX11 * has no special meaning. */ basic_string(const _CharT* __s, size_type __n, - const _Alloc& __a = _Alloc()); + const _Alloc& __a = _Alloc()) + : _M_dataplus(_S_construct(__s, __s + __n, __a), __a) + { } /** * @brief Construct string as copy of a C string. @@ -3623,7 +3631,10 @@ _GLIBCXX_END_NAMESPACE_CXX11 // 3076. basic_string CTAD ambiguity template> #endif - basic_string(const _CharT* __s, const _Alloc& __a = _Alloc()); + basic_string(const _CharT* __s, const _Alloc& __a = _Alloc()) + : _M_dataplus(_S_construct(__s, __s ? __s + traits_type::length(__s) : + __s + npos, __a), __a) + { } /** * @brief Construct string as multiple characters. @@ -3631,7 +3642,9 @@ _GLIBCXX_END_NAMESPACE_CXX11 * @param __c Character to use. * @param __a Allocator to use (default is default allocator). */ - basic_string(size_type __n, _CharT __c, const _Alloc& __a = _Alloc()); + basic_string(size_type __n, _CharT __c, const _Alloc& __a = _Alloc()) + : _M_dataplus(_S_construct(__n, __c, __a), __a) + { } #if __cplusplus >= 201103L /** @@ -3640,7 +3653,7 @@ _GLIBCXX_END_NAMESPACE_CXX11 * * The newly-created string contains the exact contents of @a __str. * @a __str is a valid, but unspecified string. - **/ + */ basic_string(basic_string&& __str) #if _GLIBCXX_FULLY_DYNAMIC_STRING == 0 noexcept // FIXME C++11: should always be noexcept. @@ -3659,7 +3672,9 @@ _GLIBCXX_END_NAMESPACE_CXX11 * @param __l std::initializer_list of characters. * @param __a Allocator to use (default is default allocator). */ - basic_string(initializer_list<_CharT> __l, const _Alloc& __a = _Alloc()); + basic_string(initializer_list<_CharT> __l, const _Alloc& __a = _Alloc()) + : _M_dataplus(_S_construct(__l.begin(), __l.end(), __a), __a) + { } basic_string(const basic_string& __str, const _Alloc& __a) : _M_dataplus(__str._M_rep()->_M_grab(__a, __str.get_allocator()), __a) @@ -3689,7 +3704,9 @@ _GLIBCXX_END_NAMESPACE_CXX11 */ template basic_string(_InputIterator __beg, _InputIterator __end, - const _Alloc& __a = _Alloc()); + const _Alloc& __a = _Alloc()) + : _M_dataplus(_S_construct(__beg, __end, __a), __a) + { } #if __cplusplus >= 201703L /** @@ -3758,7 +3775,7 @@ _GLIBCXX_END_NAMESPACE_CXX11 * * The contents of @a str are moved into this string (without copying). * @a str is a valid, but unspecified string. - **/ + */ basic_string& operator=(basic_string&& __str) _GLIBCXX_NOEXCEPT_IF(allocator_traits<_Alloc>::is_always_equal::value) diff --git a/libstdc++-v3/include/bits/basic_string.tcc b/libstdc++-v3/include/bits/basic_string.tcc index 95d2fdbd6d6..6c789665160 100644 --- a/libstdc++-v3/include/bits/basic_string.tcc +++ b/libstdc++-v3/include/bits/basic_string.tcc @@ -632,20 +632,6 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION return __r->_M_refdata(); } - template - basic_string<_CharT, _Traits, _Alloc>:: - basic_string(const basic_string& __str) - : _M_dataplus(__str._M_rep()->_M_grab(_Alloc(__str.get_allocator()), - __str.get_allocator()), - __str.get_allocator()) - { } - - template - basic_string<_CharT, _Traits, _Alloc>:: - basic_string(const _Alloc& __a) - : _M_dataplus(_S_construct(size_type(), _CharT(), __a), __a) - { } - template basic_string<_CharT, _Traits, _Alloc>:: basic_string(const basic_string& __str, size_type __pos, const _Alloc& __a) @@ -677,43 +663,6 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION + __pos, __a), __a) { } - // TBD: DPG annotate - template - basic_string<_CharT, _Traits, _Alloc>:: - basic_string(const _CharT* __s, size_type __n, const _Alloc& __a) - : _M_dataplus(_S_construct(__s, __s + __n, __a), __a) - { } - - // TBD: DPG annotate - template - basic_string<_CharT, _Traits, _Alloc>:: - basic_string(const _CharT* __s, const _Alloc& __a) - : _M_dataplus(_S_construct(__s, __s ? __s + traits_type::length(__s) : - __s + npos, __a), __a) - { } - - template - basic_string<_CharT, _Traits, _Alloc>:: - basic_string(size_type __n, _CharT __c, const _Alloc& __a) - : _M_dataplus(_S_construct(__n, __c, __a), __a) - { } - - // TBD: DPG annotate - template - template - basic_string<_CharT, _Traits, _Alloc>:: - basic_string(_InputIterator __beg, _InputIterator __end, const _Alloc& __a) - : _M_dataplus(_S_construct(__beg, __end, __a), __a) - { } - -#if __cplusplus >= 201103L - template - basic_string<_CharT, _Traits, _Alloc>:: - basic_string(initializer_list<_CharT> __l, const _Alloc& __a) - : _M_dataplus(_S_construct(__l.begin(), __l.end(), __a), __a) - { } -#endif - template basic_string<_CharT, _Traits, _Alloc>& basic_string<_CharT, _Traits, _Alloc>:: -- 2.30.2