aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJonathan Wakely <jwakely@redhat.com>2016-08-01 21:12:58 +0100
committerJonathan Wakely <redi@gcc.gnu.org>2016-08-01 21:12:58 +0100
commit92d58deefa851844eddec070d3f944b163d245e6 (patch)
tree929354393bbeed0161a46cb2534ad4bf964f4007
parent3ce9aa832b64b5e953c2a16965f8d5d581787d16 (diff)
downloadgcc-92d58deefa851844eddec070d3f944b163d245e6.zip
gcc-92d58deefa851844eddec070d3f944b163d245e6.tar.gz
gcc-92d58deefa851844eddec070d3f944b163d245e6.tar.bz2
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
-rw-r--r--libstdc++-v3/ChangeLog5
-rw-r--r--libstdc++-v3/include/bits/basic_string.h36
-rw-r--r--libstdc++-v3/testsuite/21_strings/basic_string/operations/data/char/2.cc40
-rw-r--r--libstdc++-v3/testsuite/21_strings/basic_string/operations/data/wchar_t/2.cc40
4 files changed, 117 insertions, 4 deletions
diff --git a/libstdc++-v3/ChangeLog b/libstdc++-v3/ChangeLog
index c172b01..aeeb771 100644
--- a/libstdc++-v3/ChangeLog
+++ b/libstdc++-v3/ChangeLog
@@ -1,5 +1,10 @@
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.
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();
+}