From 9ddc40f4cecc6c170b8ec2d651a423f9f326a7b4 Mon Sep 17 00:00:00 2001 From: "A. Jiang" Date: Mon, 13 Nov 2023 23:22:57 +0800 Subject: [PATCH 1/2] Implement LWG-3897 `inout_ptr` will not update raw pointer to null --- stl/inc/memory | 18 ++++++++++-------- tests/std/tests/P1132R7_out_ptr/test.cpp | 13 +++++++++++++ 2 files changed, 23 insertions(+), 8 deletions(-) diff --git a/stl/inc/memory b/stl/inc/memory index 3cc2b661cad..f448b6f9a8e 100644 --- a/stl/inc/memory +++ b/stl/inc/memory @@ -4246,20 +4246,22 @@ public: _Smart_ptr.release(); } - if (!_Get_ptr()) { - return; - } - _STD apply( [this](auto&&... _Args_) { using _Sp = _Pointer_of_or<_SmartPtr, _Pointer>; if constexpr (is_pointer_v<_SmartPtr>) { _Smart_ptr = _SmartPtr(static_cast<_Sp>(_Get_ptr()), _STD forward<_ArgsT>(_Args_)...); - } else if constexpr (_Resettable_pointer<_SmartPtr, _Sp, _Pointer, _ArgsT...>) { - _Smart_ptr.reset(static_cast<_Sp>(_Get_ptr()), _STD forward<_ArgsT>(_Args_)...); } else { - static_assert(is_constructible_v<_SmartPtr, _Sp, _ArgsT...>, "(N4950 [inout.ptr.t]/11.4)"); - _Smart_ptr = _SmartPtr(static_cast<_Sp>(_Get_ptr()), _STD forward<_ArgsT>(_Args_)...); + if (!_Get_ptr()) { + return; + } + + if constexpr (_Resettable_pointer<_SmartPtr, _Sp, _Pointer, _ArgsT...>) { + _Smart_ptr.reset(static_cast<_Sp>(_Get_ptr()), _STD forward<_ArgsT>(_Args_)...); + } else { + static_assert(is_constructible_v<_SmartPtr, _Sp, _ArgsT...>, "(N4950 [inout.ptr.t]/11.4)"); + _Smart_ptr = _SmartPtr(static_cast<_Sp>(_Get_ptr()), _STD forward<_ArgsT>(_Args_)...); + } } }, _STD move(_Get_args())); diff --git a/tests/std/tests/P1132R7_out_ptr/test.cpp b/tests/std/tests/P1132R7_out_ptr/test.cpp index c73a1f0726b..ddb55846ed7 100644 --- a/tests/std/tests/P1132R7_out_ptr/test.cpp +++ b/tests/std/tests/P1132R7_out_ptr/test.cpp @@ -33,6 +33,19 @@ void test_raw_ptr() { assert(*int_ptr == 15); } + // LWG-3897 inout_ptr will not update raw pointer to null + { + const auto delete_nullify = [](int** ptr) { + delete *ptr; + *ptr = nullptr; + }; + + int* ptr_allocated = new int{}; + + delete_nullify(inout_ptr(ptr_allocated)); + + assert(ptr_allocated == nullptr); + } } void test_shared_ptr() { From c8f6d03b8bc84396e7864378ed78f6c5fc3a63ed Mon Sep 17 00:00:00 2001 From: "Stephan T. Lavavej" Date: Mon, 13 Nov 2023 14:21:19 -0800 Subject: [PATCH 2/2] Code review feedback: Simplify control flow. --- stl/inc/memory | 18 ++++++++---------- 1 file changed, 8 insertions(+), 10 deletions(-) diff --git a/stl/inc/memory b/stl/inc/memory index f448b6f9a8e..b65377e6390 100644 --- a/stl/inc/memory +++ b/stl/inc/memory @@ -4244,6 +4244,10 @@ public: ~inout_ptr_t() { if constexpr (!is_pointer_v<_SmartPtr>) { _Smart_ptr.release(); + + if (!_Get_ptr()) { + return; + } } _STD apply( @@ -4251,17 +4255,11 @@ public: using _Sp = _Pointer_of_or<_SmartPtr, _Pointer>; if constexpr (is_pointer_v<_SmartPtr>) { _Smart_ptr = _SmartPtr(static_cast<_Sp>(_Get_ptr()), _STD forward<_ArgsT>(_Args_)...); + } else if constexpr (_Resettable_pointer<_SmartPtr, _Sp, _Pointer, _ArgsT...>) { + _Smart_ptr.reset(static_cast<_Sp>(_Get_ptr()), _STD forward<_ArgsT>(_Args_)...); } else { - if (!_Get_ptr()) { - return; - } - - if constexpr (_Resettable_pointer<_SmartPtr, _Sp, _Pointer, _ArgsT...>) { - _Smart_ptr.reset(static_cast<_Sp>(_Get_ptr()), _STD forward<_ArgsT>(_Args_)...); - } else { - static_assert(is_constructible_v<_SmartPtr, _Sp, _ArgsT...>, "(N4950 [inout.ptr.t]/11.4)"); - _Smart_ptr = _SmartPtr(static_cast<_Sp>(_Get_ptr()), _STD forward<_ArgsT>(_Args_)...); - } + static_assert(is_constructible_v<_SmartPtr, _Sp, _ArgsT...>, "(N4950 [inout.ptr.t]/11.4)"); + _Smart_ptr = _SmartPtr(static_cast<_Sp>(_Get_ptr()), _STD forward<_ArgsT>(_Args_)...); } }, _STD move(_Get_args()));