aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJonathan Wakely <jwakely@redhat.com>2021-04-22 15:46:51 +0100
committerJonathan Wakely <jwakely@redhat.com>2021-04-22 15:49:27 +0100
commit55650236cd97d81f42f9fdb4f6bcb12babafe51f (patch)
tree9cc5149af0c3dd39c964afdbb3ecf58defda6375
parent3bb6a9c01f1e9b5daf9b37fca57e90804ba90d66 (diff)
downloadgcc-55650236cd97d81f42f9fdb4f6bcb12babafe51f.zip
gcc-55650236cd97d81f42f9fdb4f6bcb12babafe51f.tar.gz
gcc-55650236cd97d81f42f9fdb4f6bcb12babafe51f.tar.bz2
libstdc++: Reject std::make_shared<T[]> [PR 99006]
Prior to C++20 it should be ill-formed to use std::make_shared with an array type (and we don't support the C++20 feature to make it valid yet anyway). libstdc++-v3/ChangeLog: PR libstdc++/99006 * include/bits/shared_ptr.h (allocate_shared): Assert that _Tp is not an array type. * include/bits/shared_ptr_base.h (__allocate_shared): Likewise. * testsuite/20_util/shared_ptr/creation/99006.cc: New test.
-rw-r--r--libstdc++-v3/include/bits/shared_ptr.h2
-rw-r--r--libstdc++-v3/include/bits/shared_ptr_base.h2
-rw-r--r--libstdc++-v3/testsuite/20_util/shared_ptr/creation/99006.cc9
3 files changed, 13 insertions, 0 deletions
diff --git a/libstdc++-v3/include/bits/shared_ptr.h b/libstdc++-v3/include/bits/shared_ptr.h
index ac4aa20..d5386ad 100644
--- a/libstdc++-v3/include/bits/shared_ptr.h
+++ b/libstdc++-v3/include/bits/shared_ptr.h
@@ -857,6 +857,8 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION
inline shared_ptr<_Tp>
allocate_shared(const _Alloc& __a, _Args&&... __args)
{
+ static_assert(!is_array<_Tp>::value, "make_shared<T[]> not supported");
+
return shared_ptr<_Tp>(_Sp_alloc_shared_tag<_Alloc>{__a},
std::forward<_Args>(__args)...);
}
diff --git a/libstdc++-v3/include/bits/shared_ptr_base.h b/libstdc++-v3/include/bits/shared_ptr_base.h
index 8766b61..71099af 100644
--- a/libstdc++-v3/include/bits/shared_ptr_base.h
+++ b/libstdc++-v3/include/bits/shared_ptr_base.h
@@ -1831,6 +1831,8 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION
inline __shared_ptr<_Tp, _Lp>
__allocate_shared(const _Alloc& __a, _Args&&... __args)
{
+ static_assert(!is_array<_Tp>::value, "make_shared<T[]> not supported");
+
return __shared_ptr<_Tp, _Lp>(_Sp_alloc_shared_tag<_Alloc>{__a},
std::forward<_Args>(__args)...);
}
diff --git a/libstdc++-v3/testsuite/20_util/shared_ptr/creation/99006.cc b/libstdc++-v3/testsuite/20_util/shared_ptr/creation/99006.cc
new file mode 100644
index 0000000..d5f7a5d
--- /dev/null
+++ b/libstdc++-v3/testsuite/20_util/shared_ptr/creation/99006.cc
@@ -0,0 +1,9 @@
+// FIXME: This should use { target { ! c++20 } }
+// { dg-do compile }
+
+#include <memory>
+
+auto p = std::make_shared<int[]>(2); // { dg-error "here" }
+auto q = std::make_shared<int[2]>(1, 2); // { dg-error "here" }
+
+// { dg-prune-output "static assertion failed" }