From: Jonathan Wakely Date: Mon, 1 Aug 2016 20:12:58 +0000 (+0100) Subject: Add non-const std::basic_string::data() for C++17 X-Git-Url: https://git.libre-soc.org/?a=commitdiff_plain;h=92d58deefa851844eddec070d3f944b163d245e6;p=gcc.git 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. From-SVN: r238963 --- diff --git a/libstdc++-v3/ChangeLog b/libstdc++-v3/ChangeLog index c172b01c339..aeeb7716d7d 100644 --- a/libstdc++-v3/ChangeLog +++ b/libstdc++-v3/ChangeLog @@ -1,5 +1,10 @@ 2016-08-01 Jonathan Wakely + * 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. + * include/bits/basic_string.tcc: Disable explicit instantiation declarations for C++17. diff --git a/libstdc++-v3/include/bits/basic_string.h b/libstdc++-v3/include/bits/basic_string.h index 49290adf55b..59f1c64ea35 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 00000000000..7608a0dcb4f --- /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 +// . + +// { dg-options "-std=gnu++17" } + +// C++17 21.3.1.7 [string.ops] string operations + +#include +#include + +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 00000000000..3c977e05464 --- /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 +// . + +// { dg-options "-std=gnu++17" } + +// C++17 21.3.1.7 [string.ops] string operations + +#include +#include + +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(); +}