From: Jonathan Wakely Date: Tue, 15 Nov 2016 14:33:20 +0000 (+0000) Subject: Add std::string constructor for substring of string_view (LWG 2742) X-Git-Url: https://git.libre-soc.org/?a=commitdiff_plain;h=bf56b0b8384cfcc5142f24eeb8f837cc974f8119;p=gcc.git Add std::string constructor for substring of string_view (LWG 2742) * doc/xml/manual/intro.xml: Document LWG 2742 status. * doc/html/*: Regenerate. * include/bits/basic_string.h (basic_string(const T&, size_type, size_type, const Allocator&)): Add constructor for substring of basic_string_view, as per LWG 2742 but with additional constraint to fix ambiguity. * testsuite/21_strings/basic_string/cons/char/9.cc: New test. * testsuite/21_strings/basic_string/cons/wchar_t/9.cc: New test. From-SVN: r242416 --- diff --git a/libstdc++-v3/ChangeLog b/libstdc++-v3/ChangeLog index 42dedfcee52..ce4a9575a51 100644 --- a/libstdc++-v3/ChangeLog +++ b/libstdc++-v3/ChangeLog @@ -1,5 +1,14 @@ 2016-11-15 Jonathan Wakely + * doc/xml/manual/intro.xml: Document LWG 2742 status. + * doc/html/*: Regenerate. + * include/bits/basic_string.h + (basic_string(const T&, size_type, size_type, const Allocator&)): Add + constructor for substring of basic_string_view, as per LWG 2742 but + with additional constraint to fix ambiguity. + * testsuite/21_strings/basic_string/cons/char/9.cc: New test. + * testsuite/21_strings/basic_string/cons/wchar_t/9.cc: New test. + * doc/xml/manual/intro.xml: Document LWG 2748 status. * include/std/optional (optional::swap): Use is_nothrow_swappable_v for exception specification. diff --git a/libstdc++-v3/doc/html/manual/bugs.html b/libstdc++-v3/doc/html/manual/bugs.html index 122bf8f7b9f..d4a39d82e89 100644 --- a/libstdc++-v3/doc/html/manual/bugs.html +++ b/libstdc++-v3/doc/html/manual/bugs.html @@ -496,11 +496,21 @@

Avoid using dynamic_cast when it would be ill-formed.

2583: - There is no way to supply an allocator for basic_string(str, pos) + There is no way to supply an allocator for basic_string(str, pos)

Add new constructor

2684: priority_queue lacking comparator typedef

Define the value_compare typedef. +

2742: + Inconsistent string interface taking string_view + +

Add the new constructor and additionally constrain it + to avoid ambiguities with non-const charT*. +

2748: + swappable traits for optionals + +

Disable the non-member swap overload when + the contained object is not swappable.

\ No newline at end of file diff --git a/libstdc++-v3/doc/xml/manual/intro.xml b/libstdc++-v3/doc/xml/manual/intro.xml index 0df24bb5f2f..7f2586d8160 100644 --- a/libstdc++-v3/doc/xml/manual/intro.xml +++ b/libstdc++-v3/doc/xml/manual/intro.xml @@ -1107,6 +1107,14 @@ requirements of the license of GCC. Define the value_compare typedef. + 2742: + Inconsistent string interface taking string_view + + + Add the new constructor and additionally constrain it + to avoid ambiguities with non-const charT*. + + 2748: swappable traits for optionals diff --git a/libstdc++-v3/include/bits/basic_string.h b/libstdc++-v3/include/bits/basic_string.h index b80e2705c29..943e88d8722 100644 --- a/libstdc++-v3/include/bits/basic_string.h +++ b/libstdc++-v3/include/bits/basic_string.h @@ -585,13 +585,28 @@ _GLIBCXX_BEGIN_NAMESPACE_CXX11 { _M_construct(__beg, __end); } #if __cplusplus > 201402L + /** + * @brief Construct string from a substring of a string_view. + * @param __t Source string view. + * @param __pos The index of the first character to copy from __t. + * @param __n The number of characters to copy from __t. + * @param __a Allocator to use. + */ + template, + __not_>>> + basic_string(const _Tp& __t, size_type __pos, size_type __n, + const _Alloc& __a = _Alloc()) + : basic_string(__sv_type(__t).substr(__pos, __n), __a) { } + /** * @brief Construct string from a string_view. * @param __sv Source string view. * @param __a Allocator to use (default is default allocator). */ - explicit basic_string(__sv_type __sv, const _Alloc& __a = _Alloc()) - : basic_string(__sv.data(), __sv.size(), __a) {} + explicit + basic_string(__sv_type __sv, const _Alloc& __a = _Alloc()) + : basic_string(__sv.data(), __sv.size(), __a) { } #endif // C++17 /** diff --git a/libstdc++-v3/testsuite/21_strings/basic_string/cons/char/9.cc b/libstdc++-v3/testsuite/21_strings/basic_string/cons/char/9.cc new file mode 100644 index 00000000000..0024ffc8894 --- /dev/null +++ b/libstdc++-v3/testsuite/21_strings/basic_string/cons/char/9.cc @@ -0,0 +1,46 @@ +// 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 +// . + +// { dg-options "-std=gnu++17" } +// { dg-do run { target c++1z } } + +#include +#include + +void +test01() +{ + using C = char; + using string_type = std::basic_string; + using view_type = std::basic_string_view; + + std::allocator alloc; + VERIFY( string_type(view_type("string")) == "string" ); + VERIFY( string_type(view_type("string"), alloc) == "string" ); + + // LWG 2742 + VERIFY( string_type("substring", 3, 6) == "string" ); + VERIFY( string_type("substring", 3, 6, alloc) == "string" ); + VERIFY( string_type(view_type("substring"), 3, 6) == "string" ); + VERIFY( string_type(view_type("substring"), 3, 6, alloc) == "string" ); +} + +int +main() +{ + test01(); +} diff --git a/libstdc++-v3/testsuite/21_strings/basic_string/cons/wchar_t/9.cc b/libstdc++-v3/testsuite/21_strings/basic_string/cons/wchar_t/9.cc new file mode 100644 index 00000000000..bf4b4403ad1 --- /dev/null +++ b/libstdc++-v3/testsuite/21_strings/basic_string/cons/wchar_t/9.cc @@ -0,0 +1,46 @@ +// 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 +// . + +// { dg-options "-std=gnu++17" } +// { dg-do run { target c++1z } } + +#include +#include + +void +test01() +{ + using C = wchar_t; + using string_type = std::basic_string; + using view_type = std::basic_string_view; + + std::allocator alloc; + VERIFY( string_type(view_type(L"string")) == L"string" ); + VERIFY( string_type(view_type(L"string"), alloc) == L"string" ); + + // LWG 2742 + VERIFY( string_type(L"substring", 3, 6) == L"string" ); + VERIFY( string_type(L"substring", 3, 6, alloc) == L"string" ); + VERIFY( string_type(view_type(L"substring"), 3, 6) == L"string" ); + VERIFY( string_type(view_type(L"substring"), 3, 6, alloc) == L"string" ); +} + +int +main() +{ + test01(); +}