* 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
2016-08-01 Jonathan Wakely <jwakely@redhat.com>
+ * 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.
/**
* @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.
*/
/**
* @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.
*/
--- /dev/null
+// 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();
+}
--- /dev/null
+// 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();
+}