Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement LWG-3734 Inconsistency in inout_ptr and out_ptr for empty case #3503

Merged
merged 3 commits into from
Mar 3, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 9 additions & 1 deletion stl/inc/memory
Original file line number Diff line number Diff line change
Expand Up @@ -4179,7 +4179,15 @@ public:
explicit out_ptr_t(_SmartPtr& _Smart_ptr_, _ArgsT... _Args_) noexcept(
is_nothrow_constructible_v<tuple<_ArgsT...>, _ArgsT...>) /* strengthened */
: _Smart_ptr(_Smart_ptr_),
_Mypair(_One_then_variadic_args_t{}, tuple<_ArgsT...>{_STD forward<_ArgsT>(_Args_)...}) {}
_Mypair(_One_then_variadic_args_t{}, tuple<_ArgsT...>{_STD forward<_ArgsT>(_Args_)...}) {
constexpr bool _Is_resettable = requires { _Smart_ptr_.reset(); }; // TRANSITION, DevCom-10291456
if constexpr (_Is_resettable) {
_Smart_ptr_.reset();
StephanTLavavej marked this conversation as resolved.
Show resolved Hide resolved
} else {
static_assert(is_constructible_v<_SmartPtr>, "the adapted pointer type must be default constructible.");
_Smart_ptr = _SmartPtr{};
CaseyCarter marked this conversation as resolved.
Show resolved Hide resolved
}
}

out_ptr_t(const out_ptr_t&) = delete;

Expand Down
32 changes: 31 additions & 1 deletion tests/std/tests/P1132R7_out_ptr/test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -75,9 +75,22 @@ void test_shared_ptr() {
assert(count == 1);
assert(*int_ptr == 32);
}
// LWG-3734 Inconsistency in inout_ptr and out_ptr for empty case
{
StephanTLavavej marked this conversation as resolved.
Show resolved Hide resolved
const auto f = [](void** ptr) { *ptr = new int(42); };

{
auto temp_adaptor = out_ptr(int_ptr, deleter);
assert(int_ptr.get() == nullptr);
f(temp_adaptor);
}

assert(count == 2);
assert(*int_ptr == 42);
}

int_ptr.reset();
assert(count == 2);
assert(count == 3);
}

template <class Ptr, class Pointer = void, class... Args>
Expand Down Expand Up @@ -118,6 +131,18 @@ void test_smart_ptr(Args&&... args) {

assert(*int_ptr == 19);
}
// LWG-3734 Inconsistency in inout_ptr and out_ptr for empty case
{
const auto f = [](void** ptr) { *ptr = new int(42); };

{
auto temp_adaptor = out_ptr<int*>(int_ptr);
assert(int_ptr.get() == nullptr);
f(temp_adaptor);
}

assert(*int_ptr == 42);
}

// LWG-3594 inout_ptr - inconsistent release() in destructor
{
Expand All @@ -141,6 +166,10 @@ struct resettable_ptr {

explicit resettable_ptr(int* p) : ptr(p) {}

void reset() {
ptr.reset();
}

void reset(int* p, reset_tag) {
ptr.reset(p);
}
Expand All @@ -163,6 +192,7 @@ struct constructible_ptr {

unique_ptr<int> ptr;

constructible_ptr() = default;
explicit constructible_ptr(int* p) : ptr(p) {}
explicit constructible_ptr(int* p, reset_tag) : ptr(p) {}

Expand Down