diff options
author | Jonathan Wakely <jwakely@redhat.com> | 2022-01-05 15:16:33 +0000 |
---|---|---|
committer | Jonathan Wakely <jwakely@redhat.com> | 2022-01-05 15:31:04 +0000 |
commit | 6aa0859afaf28f4fb13121352225bc5877e02a44 (patch) | |
tree | a601c01e4b0752a19f48161cfd77a92870934e64 /libstdc++-v3/include/bits | |
parent | 3633cc54284450433b81f0340483e15df1a49a3c (diff) | |
download | gcc-6aa0859afaf28f4fb13121352225bc5877e02a44.zip gcc-6aa0859afaf28f4fb13121352225bc5877e02a44.tar.gz gcc-6aa0859afaf28f4fb13121352225bc5877e02a44.tar.bz2 |
libstdc++: Fix overconstrained std::string constructor [PR103919]
The C++17 basic_string(const T&, size_t, size_t) constructor is
overconstrained, so it can't be used for a NTBS and a temporary string
gets constructed (potentially allocating memory). There is no
corresponding constructor taking an NTBS, so no need to disambiguate
from it. Accepting an NTBS avoids the temporary (and potential
allocation) and is what the standard requires.
libstdc++-v3/ChangeLog:
PR libstdc++/103919
* include/bits/basic_string.h (basic_string(const T&, size_t, size_t)):
Relax constraints on string_view parameter.
* include/bits/cow_string.h (basic_string(const T&, size_t, size_t)):
Likewise.
* testsuite/21_strings/basic_string/cons/char/103919.cc: New test.
Diffstat (limited to 'libstdc++-v3/include/bits')
-rw-r--r-- | libstdc++-v3/include/bits/basic_string.h | 3 | ||||
-rw-r--r-- | libstdc++-v3/include/bits/cow_string.h | 3 |
2 files changed, 4 insertions, 2 deletions
diff --git a/libstdc++-v3/include/bits/basic_string.h b/libstdc++-v3/include/bits/basic_string.h index 463cef2..a91ba51 100644 --- a/libstdc++-v3/include/bits/basic_string.h +++ b/libstdc++-v3/include/bits/basic_string.h @@ -766,7 +766,8 @@ _GLIBCXX_BEGIN_NAMESPACE_CXX11 * @param __n The number of characters to copy from __t. * @param __a Allocator to use. */ - template<typename _Tp, typename = _If_sv<_Tp, void>> + template<typename _Tp, + typename = enable_if_t<is_convertible_v<const _Tp&, __sv_type>>> _GLIBCXX20_CONSTEXPR basic_string(const _Tp& __t, size_type __pos, size_type __n, const _Alloc& __a = _Alloc()) diff --git a/libstdc++-v3/include/bits/cow_string.h b/libstdc++-v3/include/bits/cow_string.h index 8d0b772..84aab2f 100644 --- a/libstdc++-v3/include/bits/cow_string.h +++ b/libstdc++-v3/include/bits/cow_string.h @@ -690,7 +690,8 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION * @param __n The number of characters to copy from __t. * @param __a Allocator to use. */ - template<typename _Tp, typename = _If_sv<_Tp, void>> + template<typename _Tp, + typename = enable_if_t<is_convertible_v<const _Tp&, __sv_type>>> basic_string(const _Tp& __t, size_type __pos, size_type __n, const _Alloc& __a = _Alloc()) : basic_string(_S_to_string_view(__t).substr(__pos, __n), __a) { } |