Index: include/memory =================================================================== --- include/memory +++ include/memory @@ -2274,6 +2274,8 @@ template struct _LIBCPP_TEMPLATE_VIS default_delete { + static_assert(!is_function<_Tp>::value, + "default_delete cannot be instantiated for function types"); #ifndef _LIBCPP_HAS_NO_DEFAULTED_FUNCTIONS _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR default_delete() _NOEXCEPT = default; #else @@ -3493,6 +3495,17 @@ __a.deallocate(_PTraits::pointer_to(*this), 1); } +struct __shared_ptr_dummy_rebind_allocator_type; +template <> +class _LIBCPP_TEMPLATE_VIS allocator<__shared_ptr_dummy_rebind_allocator_type> +{ + template + struct rebind + { + typedef allocator<_Other> other; + }; +}; + template class _LIBCPP_TEMPLATE_VIS enable_shared_from_this; template @@ -3761,6 +3774,17 @@ #endif // _LIBCPP_HAS_NO_VARIADICS private: + template ::value> + struct __shared_ptr_default_allocator + { + typedef allocator<_Yp> type; + }; + + template + struct __shared_ptr_default_allocator<_Yp, true> + { + typedef allocator<__shared_ptr_dummy_rebind_allocator_type> type; + }; template _LIBCPP_INLINE_VISIBILITY @@ -3776,8 +3800,7 @@ } } - _LIBCPP_INLINE_VISIBILITY - void __enable_weak_this(const volatile void*, const volatile void*) _NOEXCEPT {} + _LIBCPP_INLINE_VISIBILITY void __enable_weak_this(...) _NOEXCEPT {} template friend class _LIBCPP_TEMPLATE_VIS shared_ptr; template friend class _LIBCPP_TEMPLATE_VIS weak_ptr; @@ -3808,8 +3831,9 @@ : __ptr_(__p) { unique_ptr<_Yp> __hold(__p); - typedef __shared_ptr_pointer<_Yp*, default_delete<_Yp>, allocator<_Yp> > _CntrlBlk; - __cntrl_ = new _CntrlBlk(__p, default_delete<_Yp>(), allocator<_Yp>()); + typedef typename __shared_ptr_default_allocator<_Yp>::type _AllocT; + typedef __shared_ptr_pointer<_Yp*, default_delete<_Yp>, _AllocT > _CntrlBlk; + __cntrl_ = new _CntrlBlk(__p, default_delete<_Yp>(), _AllocT()); __hold.release(); __enable_weak_this(__p, __p); } @@ -3824,8 +3848,9 @@ try { #endif // _LIBCPP_NO_EXCEPTIONS - typedef __shared_ptr_pointer<_Yp*, _Dp, allocator<_Yp> > _CntrlBlk; - __cntrl_ = new _CntrlBlk(__p, __d, allocator<_Yp>()); + typedef typename __shared_ptr_default_allocator<_Yp>::type _AllocT; + typedef __shared_ptr_pointer<_Yp*, _Dp, _AllocT > _CntrlBlk; + __cntrl_ = new _CntrlBlk(__p, __d, _AllocT()); __enable_weak_this(__p, __p); #ifndef _LIBCPP_NO_EXCEPTIONS } @@ -3846,8 +3871,9 @@ try { #endif // _LIBCPP_NO_EXCEPTIONS - typedef __shared_ptr_pointer > _CntrlBlk; - __cntrl_ = new _CntrlBlk(__p, __d, allocator<_Tp>()); + typedef typename __shared_ptr_default_allocator<_Tp>::type _AllocT; + typedef __shared_ptr_pointer _CntrlBlk; + __cntrl_ = new _CntrlBlk(__p, __d, _AllocT()); #ifndef _LIBCPP_NO_EXCEPTIONS } catch (...) @@ -4015,8 +4041,9 @@ else #endif { - typedef __shared_ptr_pointer<_Yp*, _Dp, allocator<_Yp> > _CntrlBlk; - __cntrl_ = new _CntrlBlk(__r.get(), __r.get_deleter(), allocator<_Yp>()); + typedef typename __shared_ptr_default_allocator<_Yp>::type _AllocT; + typedef __shared_ptr_pointer<_Yp*, _Dp, _AllocT > _CntrlBlk; + __cntrl_ = new _CntrlBlk(__r.get(), __r.get_deleter(), _AllocT()); __enable_weak_this(__r.get(), __r.get()); } __r.release(); @@ -4044,10 +4071,11 @@ else #endif { + typedef typename __shared_ptr_default_allocator<_Yp>::type _AllocT; typedef __shared_ptr_pointer<_Yp*, reference_wrapper::type>, - allocator<_Yp> > _CntrlBlk; - __cntrl_ = new _CntrlBlk(__r.get(), ref(__r.get_deleter()), allocator<_Yp>()); + _AllocT > _CntrlBlk; + __cntrl_ = new _CntrlBlk(__r.get(), ref(__r.get_deleter()), _AllocT()); __enable_weak_this(__r.get(), __r.get()); } __r.release(); Index: test/libcxx/utilities/memory/util.smartptr/util.smartptr.shared/function_type_default_deleter.fail.cpp =================================================================== --- test/libcxx/utilities/memory/util.smartptr/util.smartptr.shared/function_type_default_deleter.fail.cpp +++ test/libcxx/utilities/memory/util.smartptr/util.smartptr.shared/function_type_default_deleter.fail.cpp @@ -0,0 +1,40 @@ +#include + +template struct Tag {}; + +template +using SPtr = std::shared_ptr)>; + +template +using FnType = void(Tag); + +template +void TestFn(Tag) {} + +template +FnType* getFn() { + return &TestFn; +} + +struct Deleter { + template + void operator()(Tp) const { + using RawT = typename std::remove_pointer::type; + static_assert(std::is_function::value || + std::is_null_pointer::value, ""); + } +}; + +int main() { + { + SPtr<0> s; // OK + SPtr<1> s1(nullptr); // OK + SPtr<2> s2(getFn<2>(), Deleter{}); // OK + SPtr<3> s3(nullptr, Deleter{}); // OK + } + // expected-error@memory:* 2 {{static_assert failed "default_delete cannot be instantiated for function types"}} + { + SPtr<4> s4(getFn<4>()); // expected-note {{requested here}} + SPtr<5> s5(getFn<5>(), std::default_delete>{}); // expected-note {{requested here}} + } +} Index: test/std/utilities/memory/util.smartptr/util.smartptr.shared/util.smartptr.shared.create/make_shared.pass.cpp =================================================================== --- test/std/utilities/memory/util.smartptr/util.smartptr.shared/util.smartptr.shared.create/make_shared.pass.cpp +++ test/std/utilities/memory/util.smartptr/util.smartptr.shared/util.smartptr.shared.create/make_shared.pass.cpp @@ -45,6 +45,13 @@ virtual ~Foo() = default; }; +struct Result {}; +static Result theFunction() { return Result(); } +static int resultDeletorCount; +static void resultDeletor(Result (*pf)()) { + assert(pf == theFunction); + ++resultDeletorCount; +} int main() { @@ -65,7 +72,11 @@ std::shared_ptr p2 = std::make_shared(); assert(p2.get()); } - + { // https://bugs.llvm.org/show_bug.cgi?id=27566 + std::shared_ptr x(&theFunction, &resultDeletor); + std::shared_ptr y(theFunction, resultDeletor); + } + assert(resultDeletorCount == 2); #if TEST_STD_VER >= 11 nc = globalMemCounter.outstanding_new; {