This is a minimal version of the change I reverted recently. This adds the non-const data() overload, without supporting fancy pointers for allocator_type::pointer and without needing to export the new function (because we suppress the explicit instantiation declarations for C++17 now).
* include/bits/basic_string.h (data() const): Update comment. (data()): Add non-const overload for C++17. * testsuite/21_strings/basic_string/operations/data/char/2.cc: New. * testsuite/21_strings/basic_string/operations/data/wchar_t/2.cc: New. Tested powerpc64-linux, committed to trunk.
commit f03b0d6c21528ee11a0e46c3e9393ce1ebfae990 Author: Jonathan Wakely <jwak...@redhat.com> Date: Mon Aug 1 18:33:20 2016 +0100 Add non-const std::basic_string::data() for C++17 * include/bits/basic_string.h (data() const): Update comment. (data()): Add non-const overload for C++17. * testsuite/21_strings/basic_string/operations/data/char/2.cc: New. * testsuite/21_strings/basic_string/operations/data/wchar_t/2.cc: New. diff --git a/libstdc++-v3/include/bits/basic_string.h b/libstdc++-v3/include/bits/basic_string.h index 49290ad..59f1c64 100644 --- a/libstdc++-v3/include/bits/basic_string.h +++ b/libstdc++-v3/include/bits/basic_string.h @@ -2159,13 +2159,27 @@ _GLIBCXX_BEGIN_NAMESPACE_CXX11 /** * @brief Return const pointer to contents. * - * This is a handle to internal data. Do not modify or dire things may - * happen. + * This is a pointer to internal data. It is undefined to modify + * the contents through the returned pointer. To get a pointer that + * allows modifying the contents use @c &str[0] instead, + * (or in C++17 the non-const @c str.data() overload). */ const _CharT* data() const _GLIBCXX_NOEXCEPT { return _M_data(); } +#if __cplusplus > 201402L + /** + * @brief Return non-const pointer to contents. + * + * This is a pointer to the character sequence held by the string. + * Modifying the characters in the sequence is allowed. + */ + _CharT* + data() noexcept + { return _M_data(); } +#endif + /** * @brief Return copy of allocator used to construct this string. */ @@ -4658,13 +4672,27 @@ _GLIBCXX_END_NAMESPACE_CXX11 /** * @brief Return const pointer to contents. * - * This is a handle to internal data. Do not modify or dire things may - * happen. + * This is a pointer to internal data. It is undefined to modify + * the contents through the returned pointer. To get a pointer that + * allows modifying the contents use @c &str[0] instead, + * (or in C++17 the non-const @c str.data() overload). */ const _CharT* data() const _GLIBCXX_NOEXCEPT { return _M_data(); } +#if __cplusplus > 201402L + /** + * @brief Return non-const pointer to contents. + * + * This is a pointer to the character sequence held by the string. + * Modifying the characters in the sequence is allowed. + */ + _CharT* + data() noexcept + { return _M_data(); } +#endif + /** * @brief Return copy of allocator used to construct this string. */ diff --git a/libstdc++-v3/testsuite/21_strings/basic_string/operations/data/char/2.cc b/libstdc++-v3/testsuite/21_strings/basic_string/operations/data/char/2.cc new file mode 100644 index 0000000..7608a0d --- /dev/null +++ b/libstdc++-v3/testsuite/21_strings/basic_string/operations/data/char/2.cc @@ -0,0 +1,40 @@ +// 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/>. + +// { dg-options "-std=gnu++17" } + +// C++17 21.3.1.7 [string.ops] string operations + +#include <string> +#include <testsuite_hooks.h> + +void +test01() +{ + std::string s; + char* p = s.data(); + VERIFY( *p == '\0' ); + s = "a string that is longer than a short string"; + p = s.data(); + VERIFY( p == &s.front() ); +} + +int +main() +{ + test01(); +} diff --git a/libstdc++-v3/testsuite/21_strings/basic_string/operations/data/wchar_t/2.cc b/libstdc++-v3/testsuite/21_strings/basic_string/operations/data/wchar_t/2.cc new file mode 100644 index 0000000..3c977e0 --- /dev/null +++ b/libstdc++-v3/testsuite/21_strings/basic_string/operations/data/wchar_t/2.cc @@ -0,0 +1,40 @@ +// 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/>. + +// { dg-options "-std=gnu++17" } + +// C++17 21.3.1.7 [string.ops] string operations + +#include <string> +#include <testsuite_hooks.h> + +void +test01() +{ + std::wstring s; + wchar_t* p = s.data(); + VERIFY( *p == L'\0' ); + s = L"a string that is longer than a short string"; + p = s.data(); + VERIFY( p == &s.front() ); +} + +int +main() +{ + test01(); +}