Open christianbrugger opened 1 year ago
Hi @christianbrugger, thanks for the question; I'll bring it up in the next maintainers' sync.
Maintainers call: Yes, there should be a homogeneous swap
. We should add a member not_null<T>::swap(not_null<T>&)
and specialize template<typename T> std::swap(not_null<T>&, not_null<T>&)
.
and specialize
template<typename T> std::swap(not_null<T>&, not_null<T>&)
.
Wait, is specializing function templates from namespace std
something we do? I thought that was outlawed as a consequence of P0551. The current draft only mentions that class templates from std
may be specialized.
Edit: the cppreference.com documentation on std::swap discusses this:
Specializations
std::swap may be specialized in namespace std for program- defined types, but such specializations are not found by ADL (the namespace std is not the associated namespace for the program-defined type). (until C++20)
The expected way to make a program-defined type swappable is to provide a non-member function swap in the same namespace as the type: see Swappable for details.
Thanks! I think you're correct, we should overload (not specialize) in gsl::
(not std::
).
I want to exchange the content of two
gsl::not_null<std::unique_ptr<int>>
.I was pretty sure a swap would work, but couldn't get it working.
https://godbolt.org/z/64na6e356
This gives an error:
I understand that its not possible to move from such a pointer. However, not even enabling swap for smart-pointers makes
std::not_null
even less useful.Is there anything against implementing a custom
swap
, that takes twonot_null
pointers and swaps the underlying, as we know they are both not null?