[libcxx-commits] [PATCH] D57403: Extending make_shared to Support Arrays (Partially) - P0674R1
Zoe Carver via Phabricator via libcxx-commits
libcxx-commits at lists.llvm.org
Tue Jan 29 10:58:09 PST 2019
zoecarver created this revision.
zoecarver added a reviewer: EricWF.
Herald added subscribers: libcxx-commits, ldionne, christof.
Hi there,
I am a new contributor to libc++, so this patch will probably need some help. This patch incorporates some of the details highlighted in P0674R1 <http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2017/p0674r1.html>. If all goes well, I will submit another one (or update this one) with the rest of the changes.
**Example**
shared_ptr<double[1024]> p = make_shared<double[1024]>();
**Questions**
1. Is it okay to define `__shared_ptr_emplace__destroy`, a helper function, where and how I did?
2. Was removing `is_constructible<_Tp, _Args...>::value` okay (see comment in code)?
3. What are the next steps? Do I need to write more tests? Do I need to write some documentation?
Repository:
rCXX libc++
https://reviews.llvm.org/D57403
Files:
include/memory
test/libcxx/memory/shared_ptr_array.pass.cpp
Index: test/libcxx/memory/shared_ptr_array.pass.cpp
===================================================================
--- test/libcxx/memory/shared_ptr_array.pass.cpp
+++ test/libcxx/memory/shared_ptr_array.pass.cpp
@@ -0,0 +1,35 @@
+//===----------------------------------------------------------------------===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===----------------------------------------------------------------------===//
+//
+
+// <memory>
+// UNSUPPORTED: c++98, c++03, c++11, c++14, c++17
+
+#include <memory>
+#include <string>
+
+#include "test_macros.h"
+#include "min_allocator.h"
+#include "test_allocator.h"
+
+template <typename T>
+void test_make_shared()
+{
+ std::shared_ptr<T[64]> foo = std::make_shared<T[64]>();
+ std::shared_ptr<T[64]> bar(foo);
+ assert(typeid(**foo) == typeid(T) && "Type is not the same");
+ assert(typeid(**bar) == typeid(T) && "Type is not the same");
+}
+
+
+int main()
+{
+ test_make_shared<char>();
+ test_make_shared<int>();
+ test_make_shared<std::string>();
+}
Index: include/memory
===================================================================
--- include/memory
+++ include/memory
@@ -3702,11 +3702,25 @@
_Tp* get() _NOEXCEPT {return _VSTD::addressof(__data_.second());}
};
+template <typename _Tp>
+void
+__shared_ptr_emplace__destroy(_Tp& t)
+{
+ t.~_Tp();
+}
+
+template <typename _Tp, std::size_t _N>
+void
+__shared_ptr_emplace__destroy(_Tp (&t)[_N])
+{
+ for (auto i = _N; i > 0; i--) __shared_ptr_emplace__destroy(t[i]);
+}
+
template <class _Tp, class _Alloc>
void
__shared_ptr_emplace<_Tp, _Alloc>::__on_zero_shared() _NOEXCEPT
{
- __data_.second().~_Tp();
+ __shared_ptr_emplace__destroy(__data_.second());
}
template <class _Tp, class _Alloc>
@@ -4340,7 +4354,8 @@
shared_ptr<_Tp>
shared_ptr<_Tp>::allocate_shared(const _Alloc& __a, _Args&& ...__args)
{
- static_assert( is_constructible<_Tp, _Args...>::value, "Can't construct object in allocate_shared" );
+ // XXX is it okay to delete the line below? It seems like it is only checking if _Tp is an array.
+ // static_assert( is_constructible<_Tp, _Args...>::value, "Can't construct object in allocate_shared" );
typedef __shared_ptr_emplace<_Tp, _Alloc> _CntrlBlk;
typedef typename __allocator_traits_rebind<_Alloc, _CntrlBlk>::type _A2;
typedef __allocator_destructor<_A2> _D2;
@@ -4699,11 +4714,7 @@
template<class _Tp, class ..._Args>
inline _LIBCPP_INLINE_VISIBILITY
-typename enable_if
-<
- !is_array<_Tp>::value,
- shared_ptr<_Tp>
->::type
+shared_ptr<_Tp>
make_shared(_Args&& ...__args)
{
return shared_ptr<_Tp>::make_shared(_VSTD::forward<_Args>(__args)...);
-------------- next part --------------
A non-text attachment was scrubbed...
Name: D57403.184022.patch
Type: text/x-patch
Size: 2866 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/libcxx-commits/attachments/20190129/e4e592b7/attachment.bin>
More information about the libcxx-commits
mailing list