[v3 PATCH] Implement LWG 2758.
Ville Voutilainen
ville.voutilainen@gmail.com
Wed Aug 10 07:50:00 GMT 2016
Tested on Linux-x64.
2016-08-10 Ville Voutilainen <ville.voutilainen@gmail.com>
Implement LWG 2758.
* include/bits/basic_string.h
(append(__sv_type, size_type, size_type)): Turn into a template,
change parameter type, constrain, add a conversion to __sv_type
from the dependent parameter type.
(assign(__sv_type, size_type, size_type)): Likewise.
(insert(size_type, __sv_type, size_type, size_type)): Likewise.
(replace(size_type, size_type, __sv_type, size_type, size_type)):
Likewise.
(compare(size_type, size_type,__sv_type, size_type, size_type)):
Likewise.
* testsuite/21_strings/basic_string/lwg2758.cc: New.
-------------- next part --------------
diff --git a/libstdc++-v3/include/bits/basic_string.h b/libstdc++-v3/include/bits/basic_string.h
index 59f1c64..89e2100 100644
--- a/libstdc++-v3/include/bits/basic_string.h
+++ b/libstdc++-v3/include/bits/basic_string.h
@@ -1227,9 +1227,13 @@ _GLIBCXX_BEGIN_NAMESPACE_CXX11
* @param __n The number of characters to append from the string_view.
* @return Reference to this string.
*/
- basic_string& append(__sv_type __sv,
+ template <typename _Tp,
+ enable_if_t<is_convertible_v<const _Tp&, __sv_type>,
+ bool> = true>
+ basic_string& append(const _Tp& __svt,
size_type __pos, size_type __n = npos)
{
+ __sv_type __sv = __svt;
return _M_append(__sv.data()
+ __sv._M_check(__pos, "basic_string::append"),
__sv._M_limit(__pos, __n));
@@ -1392,10 +1396,14 @@ _GLIBCXX_BEGIN_NAMESPACE_CXX11
* @param __n The number of characters to assign.
* @return Reference to this string.
*/
+ template <typename _Tp,
+ enable_if_t<is_convertible_v<const _Tp&, __sv_type>,
+ bool> = true>
basic_string&
- assign(__sv_type __sv,
+ assign(const _Tp& __svt,
size_type __pos, size_type __n = npos)
{
+ __sv_type __sv = __svt;
return _M_replace(size_type(0), this->size(), __sv.data()
+ __sv._M_check(__pos, "basic_string::assign"),
__sv._M_limit(__pos, __n));
@@ -1652,9 +1660,13 @@ _GLIBCXX_BEGIN_NAMESPACE_CXX11
* @param __n The number of characters to insert.
* @return Reference to this string.
*/
- basic_string& insert(size_type __pos1, __sv_type __sv,
+ template <typename _Tp,
+ enable_if_t<is_convertible_v<const _Tp&, __sv_type>,
+ bool> = true>
+ basic_string& insert(size_type __pos1, const _Tp& __svt,
size_type __pos2, size_type __n = npos)
{
+ __sv_type __sv = __svt;
return this->replace(__pos1, size_type(0), __sv.data()
+ __sv._M_check(__pos2, "basic_string::insert"),
__sv._M_limit(__pos2, __n));
@@ -2071,10 +2083,14 @@ _GLIBCXX_BEGIN_NAMESPACE_CXX11
* @param __n2 The number of characters to insert.
* @return Reference to this string.
*/
+ template <typename _Tp,
+ enable_if_t<is_convertible_v<const _Tp&, __sv_type>,
+ bool> = true>
basic_string& replace(size_type __pos1, size_type __n1,
- __sv_type __sv,
+ const _Tp& __svt,
size_type __pos2, size_type __n2 = npos)
{
+ __sv_type __sv = __svt;
return this->replace(__pos1, __n1, __sv.data()
+ __sv._M_check(__pos2, "basic_string::replace"),
__sv._M_limit(__pos2, __n2));
@@ -2720,10 +2736,14 @@ _GLIBCXX_BEGIN_NAMESPACE_CXX11
* @param __n2 The number of characters to compare.
* @return Integer < 0, 0, or > 0.
*/
+ template <typename _Tp,
+ enable_if_t<is_convertible_v<const _Tp&, __sv_type>,
+ bool> = true>
int compare(size_type __pos1, size_type __n1,
- __sv_type __sv,
+ const _Tp& __svt,
size_type __pos2, size_type __n2 = npos) const
{
+ __sv_type __sv = __svt;
return __sv_type(*this)
.substr(__pos1, __n1).compare(__sv.substr(__pos2, __n2));
}
diff --git a/libstdc++-v3/testsuite/21_strings/basic_string/lwg2758.cc b/libstdc++-v3/testsuite/21_strings/basic_string/lwg2758.cc
new file mode 100644
index 0000000..1d29248
--- /dev/null
+++ b/libstdc++-v3/testsuite/21_strings/basic_string/lwg2758.cc
@@ -0,0 +1,46 @@
+// { dg-options "-std=gnu++17" }
+// { dg-do compile }
+
+// Copyright (C) 2016 Free Software Foundation, Inc.
+//
+// This file is part of the GNU ISO C++ Library. This library is free
+// software; you can redistribute it and/or modify it under the
+// terms of the GNU General Public License as published by the
+// Free Software Foundation; either version 3, or (at your option)
+// any later version.
+
+// This library is distributed in the hope that it will be useful,
+// but WITHOUT ANY WARRANTY; without even the implied warranty of
+// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+// GNU General Public License for more details.
+
+// You should have received a copy of the GNU General Public License along
+// with this library; see the file COPYING3. If not see
+// <http://www.gnu.org/licenses/>.
+
+#include <string>
+
+struct CustomString
+{
+ std::string data = "foo";
+ std::string_view data_view = data;
+ operator std::string_view() const {return data_view;}
+};
+
+int main()
+{
+ std::string x;
+ CustomString cs;
+ x.append("foo", 0, 3);
+ x.append(cs, 0, 3);
+ x.assign("foo", 0, 3);
+ x.assign(cs, 0, 3);
+ x.insert(0, "foo", 0, 3);
+ x.insert(0, cs, 0, 3);
+ x = "bar";
+ x.replace(0, 3, "foo", 0, 3);
+ x.replace(0, 3, cs, 0, 3);
+ x = "bar";
+ x.compare(0, 3, "foo", 0, 3);
+ x.compare(0, 3, cs, 0, 3);
+}
More information about the Libstdc++
mailing list