5454#include < __type_traits/remove_extent.h>
5555#include < __type_traits/remove_reference.h>
5656#include < __utility/declval.h>
57+ #include < __utility/exception_guard.h>
5758#include < __utility/forward.h>
5859#include < __utility/move.h>
5960#include < __utility/swap.h>
@@ -352,52 +353,38 @@ class _LIBCPP_SHARED_PTR_TRIVIAL_ABI shared_ptr {
352353
353354 template <class _Yp , class _Dp , __enable_if_t <__shared_ptr_deleter_ctor_reqs<_Dp, _Yp, _Tp>::value, int > = 0 >
354355 _LIBCPP_HIDE_FROM_ABI shared_ptr (_Yp* __p, _Dp __d) : __ptr_(__p) {
355- #if _LIBCPP_HAS_EXCEPTIONS
356- try {
357- #endif // _LIBCPP_HAS_EXCEPTIONS
358- typedef typename __shared_ptr_default_allocator<_Yp>::type _AllocT;
359- typedef __shared_ptr_pointer<_Yp*, _Dp, _AllocT> _CntrlBlk;
356+ auto __guard = std::__make_exception_guard ([&] { __d (__p); });
357+ typedef typename __shared_ptr_default_allocator<_Yp>::type _AllocT;
358+ typedef __shared_ptr_pointer<_Yp*, _Dp, _AllocT> _CntrlBlk;
360359#ifndef _LIBCPP_CXX03_LANG
361- __cntrl_ = new _CntrlBlk (__p, std::move (__d), _AllocT ());
360+ __cntrl_ = new _CntrlBlk (__p, std::move (__d), _AllocT ());
362361#else
363362 __cntrl_ = new _CntrlBlk (__p, __d, _AllocT ());
364363#endif // not _LIBCPP_CXX03_LANG
365- __enable_weak_this (__p, __p);
366- #if _LIBCPP_HAS_EXCEPTIONS
367- } catch (...) {
368- __d (__p);
369- throw ;
370- }
371- #endif // _LIBCPP_HAS_EXCEPTIONS
364+ __enable_weak_this (__p, __p);
365+ __guard.__complete ();
372366 }
373367
374368 template <class _Yp ,
375369 class _Dp ,
376370 class _Alloc ,
377371 __enable_if_t <__shared_ptr_deleter_ctor_reqs<_Dp, _Yp, _Tp>::value, int > = 0 >
378372 _LIBCPP_HIDE_FROM_ABI shared_ptr (_Yp* __p, _Dp __d, _Alloc __a) : __ptr_(__p) {
379- #if _LIBCPP_HAS_EXCEPTIONS
380- try {
381- #endif // _LIBCPP_HAS_EXCEPTIONS
382- typedef __shared_ptr_pointer<_Yp*, _Dp, _Alloc> _CntrlBlk;
383- typedef typename __allocator_traits_rebind<_Alloc, _CntrlBlk>::type _A2;
384- typedef __allocator_destructor<_A2> _D2;
385- _A2 __a2 (__a);
386- unique_ptr<_CntrlBlk, _D2> __hold2 (__a2.allocate (1 ), _D2 (__a2, 1 ));
387- ::new ((void *)std::addressof (*__hold2.get ()))
373+ auto __guard = std::__make_exception_guard ([&] { __d (__p); });
374+ typedef __shared_ptr_pointer<_Yp*, _Dp, _Alloc> _CntrlBlk;
375+ typedef typename __allocator_traits_rebind<_Alloc, _CntrlBlk>::type _A2;
376+ typedef __allocator_destructor<_A2> _D2;
377+ _A2 __a2 (__a);
378+ unique_ptr<_CntrlBlk, _D2> __hold2 (__a2.allocate (1 ), _D2 (__a2, 1 ));
379+ ::new ((void *)std::addressof (*__hold2.get ()))
388380#ifndef _LIBCPP_CXX03_LANG
389- _CntrlBlk (__p, std::move (__d), __a);
381+ _CntrlBlk (__p, std::move (__d), __a);
390382#else
391383 _CntrlBlk (__p, __d, __a);
392384#endif // not _LIBCPP_CXX03_LANG
393- __cntrl_ = std::addressof (*__hold2.release ());
394- __enable_weak_this (__p, __p);
395- #if _LIBCPP_HAS_EXCEPTIONS
396- } catch (...) {
397- __d (__p);
398- throw ;
399- }
400- #endif // _LIBCPP_HAS_EXCEPTIONS
385+ __cntrl_ = std::addressof (*__hold2.release ());
386+ __enable_weak_this (__p, __p);
387+ __guard.__complete ();
401388 }
402389
403390 template <class _Dp >
@@ -406,22 +393,15 @@ class _LIBCPP_SHARED_PTR_TRIVIAL_ABI shared_ptr {
406393 _Dp __d,
407394 __enable_if_t <__shared_ptr_nullptr_deleter_ctor_reqs<_Dp>::value, __nullptr_sfinae_tag> = __nullptr_sfinae_tag())
408395 : __ptr_(nullptr ) {
409- #if _LIBCPP_HAS_EXCEPTIONS
410- try {
411- #endif // _LIBCPP_HAS_EXCEPTIONS
412- typedef typename __shared_ptr_default_allocator<_Tp>::type _AllocT;
413- typedef __shared_ptr_pointer<nullptr_t , _Dp, _AllocT> _CntrlBlk;
396+ auto __guard = std::__make_exception_guard ([&] { __d (__p); });
397+ typedef typename __shared_ptr_default_allocator<_Tp>::type _AllocT;
398+ typedef __shared_ptr_pointer<nullptr_t , _Dp, _AllocT> _CntrlBlk;
414399#ifndef _LIBCPP_CXX03_LANG
415- __cntrl_ = new _CntrlBlk (__p, std::move (__d), _AllocT ());
400+ __cntrl_ = new _CntrlBlk (__p, std::move (__d), _AllocT ());
416401#else
417402 __cntrl_ = new _CntrlBlk (__p, __d, _AllocT ());
418403#endif // not _LIBCPP_CXX03_LANG
419- #if _LIBCPP_HAS_EXCEPTIONS
420- } catch (...) {
421- __d (__p);
422- throw ;
423- }
424- #endif // _LIBCPP_HAS_EXCEPTIONS
404+ __guard.__complete ();
425405 }
426406
427407 template <class _Dp , class _Alloc >
@@ -431,27 +411,20 @@ class _LIBCPP_SHARED_PTR_TRIVIAL_ABI shared_ptr {
431411 _Alloc __a,
432412 __enable_if_t <__shared_ptr_nullptr_deleter_ctor_reqs<_Dp>::value, __nullptr_sfinae_tag> = __nullptr_sfinae_tag())
433413 : __ptr_(nullptr ) {
434- #if _LIBCPP_HAS_EXCEPTIONS
435- try {
436- #endif // _LIBCPP_HAS_EXCEPTIONS
437- typedef __shared_ptr_pointer<nullptr_t , _Dp, _Alloc> _CntrlBlk;
438- typedef typename __allocator_traits_rebind<_Alloc, _CntrlBlk>::type _A2;
439- typedef __allocator_destructor<_A2> _D2;
440- _A2 __a2 (__a);
441- unique_ptr<_CntrlBlk, _D2> __hold2 (__a2.allocate (1 ), _D2 (__a2, 1 ));
442- ::new ((void *)std::addressof (*__hold2.get ()))
414+ auto __guard = std::__make_exception_guard ([&] { __d (__p); });
415+ typedef __shared_ptr_pointer<nullptr_t , _Dp, _Alloc> _CntrlBlk;
416+ typedef typename __allocator_traits_rebind<_Alloc, _CntrlBlk>::type _A2;
417+ typedef __allocator_destructor<_A2> _D2;
418+ _A2 __a2 (__a);
419+ unique_ptr<_CntrlBlk, _D2> __hold2 (__a2.allocate (1 ), _D2 (__a2, 1 ));
420+ ::new ((void *)std::addressof (*__hold2.get ()))
443421#ifndef _LIBCPP_CXX03_LANG
444- _CntrlBlk (__p, std::move (__d), __a);
422+ _CntrlBlk (__p, std::move (__d), __a);
445423#else
446424 _CntrlBlk (__p, __d, __a);
447425#endif // not _LIBCPP_CXX03_LANG
448- __cntrl_ = std::addressof (*__hold2.release ());
449- #if _LIBCPP_HAS_EXCEPTIONS
450- } catch (...) {
451- __d (__p);
452- throw ;
453- }
454- #endif // _LIBCPP_HAS_EXCEPTIONS
426+ __cntrl_ = std::addressof (*__hold2.release ());
427+ __guard.__complete ();
455428 }
456429
457430 template <class _Yp >
0 commit comments