diff --git a/stl/inc/memory b/stl/inc/memory index 3cc2b661cad..b65377e6390 100644 --- a/stl/inc/memory +++ b/stl/inc/memory @@ -4244,10 +4244,10 @@ public: ~inout_ptr_t() { if constexpr (!is_pointer_v<_SmartPtr>) { _Smart_ptr.release(); - } - if (!_Get_ptr()) { - return; + if (!_Get_ptr()) { + return; + } } _STD apply( 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() {