aboutsummaryrefslogtreecommitdiff
path: root/libstdc++-v3
diff options
context:
space:
mode:
authorJonathan Wakely <jwakely@redhat.com>2020-10-19 22:11:39 +0100
committerJonathan Wakely <jwakely@redhat.com>2020-10-19 22:11:39 +0100
commite7a0af84d68f50b65dffa1af462d10bd4bf03939 (patch)
treead0aa318c41652ae2c9a474134733f37331316e3 /libstdc++-v3
parentbadeac77f55276d047b3cc202e4ddd49ba643d8a (diff)
downloadgcc-e7a0af84d68f50b65dffa1af462d10bd4bf03939.zip
gcc-e7a0af84d68f50b65dffa1af462d10bd4bf03939.tar.gz
gcc-e7a0af84d68f50b65dffa1af462d10bd4bf03939.tar.bz2
libstdc++: Implement std::make_unique_for_overwrite
This is the std::unique_ptr part of P1020R1 (as amended by P1973R1) for C++20. The std::shared_ptr part still needs to be done. libstdc++-v3/ChangeLog: * include/bits/unique_ptr.h (make_unique_for_overwrite): Define for C++20. * testsuite/20_util/unique_ptr/creation/array_neg.cc: Remove unused header. Adjust standard reference. * testsuite/20_util/unique_ptr/creation/for_overwrite.cc: New test. * testsuite/20_util/unique_ptr/creation/for_overwrite__neg.cc: New test.
Diffstat (limited to 'libstdc++-v3')
-rw-r--r--libstdc++-v3/include/bits/unique_ptr.h22
-rw-r--r--libstdc++-v3/testsuite/20_util/unique_ptr/creation/array_neg.cc3
-rw-r--r--libstdc++-v3/testsuite/20_util/unique_ptr/creation/for_overwrite.cc65
-rw-r--r--libstdc++-v3/testsuite/20_util/unique_ptr/creation/for_overwrite__neg.cc34
4 files changed, 121 insertions, 3 deletions
diff --git a/libstdc++-v3/include/bits/unique_ptr.h b/libstdc++-v3/include/bits/unique_ptr.h
index d0e4cefa..252ea8991 100644
--- a/libstdc++-v3/include/bits/unique_ptr.h
+++ b/libstdc++-v3/include/bits/unique_ptr.h
@@ -969,8 +969,28 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION
/// Disable std::make_unique for arrays of known bound
template<typename _Tp, typename... _Args>
- inline typename _MakeUniq<_Tp>::__invalid_type
+ typename _MakeUniq<_Tp>::__invalid_type
make_unique(_Args&&...) = delete;
+
+#if __cplusplus > 201703L
+ /// std::make_unique_for_overwrite for single objects
+ template<typename _Tp>
+ inline typename _MakeUniq<_Tp>::__single_object
+ make_unique_for_overwrite()
+ { return unique_ptr<_Tp>(new _Tp); }
+
+ /// std::make_unique_for_overwrite for arrays of unknown bound
+ template<typename _Tp>
+ inline typename _MakeUniq<_Tp>::__array
+ make_unique_for_overwrite(size_t __n)
+ { return unique_ptr<_Tp>(new remove_extent_t<_Tp>[__n]); }
+
+ /// Disable std::make_unique_for_overwrite for arrays of known bound
+ template<typename _Tp, typename... _Args>
+ typename _MakeUniq<_Tp>::__invalid_type
+ make_unique_for_overwrite(_Args&&...) = delete;
+#endif // C++20
+
// @} relates unique_ptr
#endif // C++14
diff --git a/libstdc++-v3/testsuite/20_util/unique_ptr/creation/array_neg.cc b/libstdc++-v3/testsuite/20_util/unique_ptr/creation/array_neg.cc
index 928080d..a76cacb 100644
--- a/libstdc++-v3/testsuite/20_util/unique_ptr/creation/array_neg.cc
+++ b/libstdc++-v3/testsuite/20_util/unique_ptr/creation/array_neg.cc
@@ -17,10 +17,9 @@
// with this library; see the file COPYING3. If not see
// <http://www.gnu.org/licenses/>.
-// 20.9.1.4 unique_ptr creation [unique.ptr.create]
+// C++14 20.8.1.4 unique_ptr creation [unique.ptr.create]
#include <memory>
-#include <testsuite_hooks.h>
struct A { };
diff --git a/libstdc++-v3/testsuite/20_util/unique_ptr/creation/for_overwrite.cc b/libstdc++-v3/testsuite/20_util/unique_ptr/creation/for_overwrite.cc
new file mode 100644
index 0000000..e7231c2
--- /dev/null
+++ b/libstdc++-v3/testsuite/20_util/unique_ptr/creation/for_overwrite.cc
@@ -0,0 +1,65 @@
+// { dg-options "-std=gnu++20" }
+// { dg-do run { target c++2a } }
+
+// Copyright (C) 2020 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/>.
+
+// C++20 20.11.1.5 unique_ptr creation [unique.ptr.create]
+
+#include <memory>
+#include <cstdlib>
+#include <cstring>
+#include <testsuite_hooks.h>
+
+void* operator new(std::size_t n)
+{
+ void* p = std::malloc(n);
+ std::memset(p, 0xaa, n);
+ return p;
+}
+
+void operator delete(void* p) { std::free(p); }
+void operator delete(void* p, std::size_t) { std::free(p); }
+
+void
+test01()
+{
+ std::unique_ptr<int> a = std::make_unique_for_overwrite<int>();
+ VERIFY( a != nullptr );
+ unsigned char buf[sizeof(int)];
+ std::memcpy(buf, a.get(), sizeof(buf));
+ for (unsigned char c : buf)
+ VERIFY( c == 0xaa );
+}
+
+void
+test02()
+{
+ std::unique_ptr<int[]> a = std::make_unique_for_overwrite<int[]>(3);
+ VERIFY( a != nullptr );
+ unsigned char buf[3 * sizeof(int)];
+ std::memcpy(buf, a.get(), sizeof(buf));
+ for (unsigned char c : buf)
+ VERIFY( c == 0xaa );
+}
+
+int
+main()
+{
+ test01();
+ test02();
+}
diff --git a/libstdc++-v3/testsuite/20_util/unique_ptr/creation/for_overwrite__neg.cc b/libstdc++-v3/testsuite/20_util/unique_ptr/creation/for_overwrite__neg.cc
new file mode 100644
index 0000000..3571ae24
--- /dev/null
+++ b/libstdc++-v3/testsuite/20_util/unique_ptr/creation/for_overwrite__neg.cc
@@ -0,0 +1,34 @@
+// { dg-options "-std=gnu++20" }
+// { dg-do compile { target c++2a } }
+
+// Copyright (C) 2020 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/>.
+
+// C++20 20.11.1.5 unique_ptr creation [unique.ptr.create]
+
+#include <memory>
+
+struct A { };
+
+auto p1 = std::make_unique_for_overwrite<A>(1); // { dg-error "no matching function" }
+auto p1 = std::make_unique_for_overwrite<A[]>(); // { dg-error "no matching function" }
+auto p2 = std::make_unique_for_overwrite<A[]>(1, 2); // { dg-error "no matching function" }
+auto p3 = std::make_unique_for_overwrite<A[1]>(); // { dg-error "deleted" }
+auto p4 = std::make_unique_for_overwrite<A[1]>(1);// { dg-error "deleted" }
+
+// { dg-prune-output "declared here" }
+// { dg-prune-output "no type named" }