Skip to content

Commit e7c3b6b

Browse files
committed
[libc++] Simplify the implementation of the unique_ptr -> shared_ptr converting constructor
1 parent 9d1b6ee commit e7c3b6b

File tree

2 files changed

+8
-37
lines changed

2 files changed

+8
-37
lines changed

libcxx/include/__memory/shared_ptr.h

Lines changed: 6 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -487,45 +487,16 @@ class _LIBCPP_SHARED_PTR_TRIVIAL_ABI shared_ptr {
487487

488488
template <class _Yp,
489489
class _Dp,
490-
__enable_if_t<!is_lvalue_reference<_Dp>::value && __compatible_with<_Yp, _Tp>::value &&
490+
__enable_if_t<__compatible_with<_Yp, _Tp>::value &&
491491
is_convertible<typename unique_ptr<_Yp, _Dp>::pointer, element_type*>::value,
492492
int> = 0>
493493
_LIBCPP_HIDE_FROM_ABI shared_ptr(unique_ptr<_Yp, _Dp>&& __r) : __ptr_(__r.get()) {
494-
#if _LIBCPP_STD_VER >= 14
495-
if (__ptr_ == nullptr)
496-
__cntrl_ = nullptr;
497-
else
498-
#endif
499-
{
500-
typedef typename __shared_ptr_default_allocator<_Yp>::type _AllocT;
501-
typedef __shared_ptr_pointer<typename unique_ptr<_Yp, _Dp>::pointer, _Dp, _AllocT> _CntrlBlk;
502-
__cntrl_ = new _CntrlBlk(__r.get(), std::move(__r.get_deleter()), _AllocT());
503-
__enable_weak_this(__r.get(), __r.get());
504-
}
505-
__r.release();
506-
}
494+
using _AllocT = typename __shared_ptr_default_allocator<_Yp>::type;
495+
using _Deleter = _If<is_lvalue_reference<_Dp>::value, reference_wrapper<__libcpp_remove_reference_t<_Dp> >, _Dp>;
496+
using _CntrlBlk = __shared_ptr_pointer<typename unique_ptr<_Yp, _Dp>::pointer, _Deleter, _AllocT>;
507497

508-
template <class _Yp,
509-
class _Dp,
510-
class = void,
511-
__enable_if_t<is_lvalue_reference<_Dp>::value && __compatible_with<_Yp, _Tp>::value &&
512-
is_convertible<typename unique_ptr<_Yp, _Dp>::pointer, element_type*>::value,
513-
int> = 0>
514-
_LIBCPP_HIDE_FROM_ABI shared_ptr(unique_ptr<_Yp, _Dp>&& __r) : __ptr_(__r.get()) {
515-
#if _LIBCPP_STD_VER >= 14
516-
if (__ptr_ == nullptr)
517-
__cntrl_ = nullptr;
518-
else
519-
#endif
520-
{
521-
typedef typename __shared_ptr_default_allocator<_Yp>::type _AllocT;
522-
typedef __shared_ptr_pointer<typename unique_ptr<_Yp, _Dp>::pointer,
523-
reference_wrapper<__libcpp_remove_reference_t<_Dp> >,
524-
_AllocT>
525-
_CntrlBlk;
526-
__cntrl_ = new _CntrlBlk(__r.get(), std::ref(__r.get_deleter()), _AllocT());
527-
__enable_weak_this(__r.get(), __r.get());
528-
}
498+
__cntrl_ = __ptr_ ? new _CntrlBlk(__r.get(), std::forward<_Dp>(__r.get_deleter()), _AllocT()) : nullptr;
499+
__enable_weak_this(__r.get(), __r.get());
529500
__r.release();
530501
}
531502

libcxx/test/std/utilities/memory/util.smartptr/util.smartptr.shared/util.smartptr.shared.const/unique_ptr.pass.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@
1212

1313
// template <class Y, class D> shared_ptr(unique_ptr<Y, D>&&r);
1414

15+
// XFAIL: FROZEN-CXX03-HEADERS-FIXME
16+
1517
#include <memory>
1618
#include <new>
1719
#include <cstdlib>
@@ -165,12 +167,10 @@ int main(int, char**)
165167
{ // LWG 2399
166168
fn(std::unique_ptr<int>(new int));
167169
}
168-
#if TEST_STD_VER >= 14
169170
{ // LWG 2415
170171
std::unique_ptr<int, void (*)(int*)> p(nullptr, assert_deleter<int>);
171172
std::shared_ptr<int> p2(std::move(p)); // should not call deleter when going out of scope
172173
}
173-
#endif
174174

175175
{
176176
adl::D d;

0 commit comments

Comments
 (0)