Skip to content

Implement P1132R7 out_ptr() and inout_ptr()#1998

Merged
StephanTLavavej merged 11 commits into
microsoft:mainfrom
AdamBucior:out_ptr
Jul 30, 2021
Merged

Implement P1132R7 out_ptr() and inout_ptr()#1998
StephanTLavavej merged 11 commits into
microsoft:mainfrom
AdamBucior:out_ptr

Conversation

@AdamBucior
Copy link
Copy Markdown
Contributor

Resolves #1971.

@AdamBucior AdamBucior requested a review from a team as a code owner June 10, 2021 15:48
Comment thread stl/inc/memory Outdated
@miscco
Copy link
Copy Markdown
Contributor

miscco commented Jun 10, 2021

Awesome!

I was wondering whether I should give it a try but you beat me to it. Will Review tomorrow

Comment thread stl/inc/memory Outdated
Comment thread stl/inc/memory
@StephanTLavavej StephanTLavavej added the cxx23 C++23 feature label Jun 10, 2021
Comment thread stl/inc/memory Outdated
Comment thread stl/inc/memory Outdated
Comment thread stl/inc/memory
Comment thread stl/inc/memory Outdated
Comment thread stl/inc/memory Outdated
Comment thread stl/inc/memory Outdated
Comment thread stl/inc/memory
Comment thread stl/inc/memory Outdated
Comment thread stl/inc/memory
Comment thread stl/inc/memory Outdated

operator void**() const noexcept requires(!is_same_v<_Pointer, void*>) {
static_assert(is_pointer_v<_Pointer>, "out_ptr_t's operator void** requires Pointer to be a pointer");
return reinterpret_cast<void**>(_STD addressof(const_cast<_Pointer&>(_Ptr)));
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We have the precondition that we should not have called operator _Pointer* Do we care enough to actually validate this somehow? It would most likely cost some memory so I am totally fine with nasal demons emerging

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think it's worth validating. It will be used as foo(out_ptr(my_ptr)) 99.9% of the time.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I agree with Adam. It's not so much the memory as it is having the ABI of out_ptr differ between debug and release modes. We already do that with many types, but doing it here will probably cause more harm than good, given Adam's point about common usage

@StephanTLavavej

This comment has been minimized.

@StephanTLavavej

This comment has been minimized.

Copy link
Copy Markdown
Contributor

@barcharcraz barcharcraz left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You need to add the feature test macros, additionally I found the error messages produced by the static_asserts to be possibly confusing, but I don't care all that strongly about that.

I observe that we may be able to apply certain optimizations under the "as if" rule to this type. In particular for raw and unique ptrs. Raw pointers maybe aren't worth it, but unique_ptrs might be.

I think the optimization involves calling release in the constructor and then not storing the reference to the smart pointer itself. I'm OK with not applying such an optimization but we should keep it in mind.

Comment thread stl/inc/yvals_core.h
Comment thread stl/inc/memory
Comment thread stl/inc/memory Outdated

operator void**() const noexcept requires(!is_same_v<_Pointer, void*>) {
static_assert(is_pointer_v<_Pointer>, "out_ptr_t's operator void** requires Pointer to be a pointer");
return reinterpret_cast<void**>(_STD addressof(const_cast<_Pointer&>(_Ptr)));
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I agree with Adam. It's not so much the memory as it is having the ABI of out_ptr differ between debug and release modes. We already do that with many types, but doing it here will probably cause more harm than good, given Adam's point about common usage

Comment thread stl/inc/memory Outdated
Comment thread stl/inc/memory Outdated
Comment thread stl/inc/memory Outdated
Comment thread stl/inc/memory Outdated
Comment thread stl/inc/memory Outdated
Comment thread stl/inc/memory
Comment thread stl/inc/memory Outdated
Copy link
Copy Markdown
Member

@StephanTLavavej StephanTLavavej left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks! My major question is whether it's worth compressing away empty tuple<>s; everything else is nitpick-level.

Comment thread tests/std/tests/P1132R7_out_ptr/test.cpp
Comment thread stl/inc/memory Outdated
Comment thread stl/inc/memory Outdated
Comment thread stl/inc/memory Outdated
Comment thread stl/inc/memory Outdated
Comment thread stl/inc/memory
Comment thread stl/inc/memory Outdated
Comment thread stl/inc/memory Outdated
Comment thread tests/std/tests/P1132R7_out_ptr/test.cpp Outdated
Comment thread tests/std/tests/P1132R7_out_ptr/test.cpp Outdated
@StephanTLavavej StephanTLavavej self-assigned this Jul 13, 2021
@StephanTLavavej StephanTLavavej removed their assignment Jul 14, 2021
@StephanTLavavej
Copy link
Copy Markdown
Member

@AdamBucior @barcharcraz I've pushed a merge with main, followed by a commit to avoid clang-format 12 damaging a couple of lines.

@StephanTLavavej StephanTLavavej self-assigned this Jul 29, 2021
@StephanTLavavej StephanTLavavej merged commit b24a40d into microsoft:main Jul 30, 2021
@StephanTLavavej
Copy link
Copy Markdown
Member

Thanks for implementing this C++23 feature! 😻 🚀 🎉

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

cxx23 C++23 feature

Projects

None yet

Development

Successfully merging this pull request may close these issues.

P1132R7 out_ptr(), inout_ptr()

7 participants