Skip to content

Commit a254fda

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

File tree

2 files changed

+11
-33
lines changed

2 files changed

+11
-33
lines changed

libcxx/include/__memory/shared_ptr.h

Lines changed: 11 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -487,45 +487,25 @@ 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;
507495

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
516496
if (__ptr_ == nullptr)
517497
__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;
498+
else if constexpr (is_lvalue_reference<_Dp>::value) {
499+
using _CntrlBlk =
500+
__shared_ptr_pointer<typename unique_ptr<_Yp, _Dp>::pointer,
501+
reference_wrapper<__libcpp_remove_reference_t<_Dp> >,
502+
_AllocT>;
526503
__cntrl_ = new _CntrlBlk(__r.get(), std::ref(__r.get_deleter()), _AllocT());
527-
__enable_weak_this(__r.get(), __r.get());
504+
} else {
505+
using _CntrlBlk = __shared_ptr_pointer<typename unique_ptr<_Yp, _Dp>::pointer, _Dp, _AllocT>;
506+
__cntrl_ = new _CntrlBlk(__r.get(), std::move(__r.get_deleter()), _AllocT());
528507
}
508+
__enable_weak_this(__r.get(), __r.get());
529509
__r.release();
530510
}
531511

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

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -165,12 +165,10 @@ int main(int, char**)
165165
{ // LWG 2399
166166
fn(std::unique_ptr<int>(new int));
167167
}
168-
#if TEST_STD_VER >= 14
169168
{ // LWG 2415
170169
std::unique_ptr<int, void (*)(int*)> p(nullptr, assert_deleter<int>);
171170
std::shared_ptr<int> p2(std::move(p)); // should not call deleter when going out of scope
172171
}
173-
#endif
174172

175173
{
176174
adl::D d;

0 commit comments

Comments
 (0)