unique_ptr::operator*
should not allow dangling referencesSection: 20.3.1.3.5 [unique.ptr.single.observers] Status: Tentatively Ready Submitter: Jonathan Wakely Opened: 2024-09-02 Last modified: 2024-09-18
Priority: Not Prioritized
View other active issues in [unique.ptr.single.observers].
View all other issues in [unique.ptr.single.observers].
View all issues with Tentatively Ready status.
Discussion:
If unique_ptr<T,D>::element_type*
and D::pointer
are not the same type, it's possible for operator*()
to return a dangling
reference that has undefined behaviour.
struct deleter {
using pointer = long*;
void operator()(pointer) const {}
};
long l = 0;
std::unique_ptr<const int, deleter> p(&l);
int i = *p; // undefined
We should make this case ill-formed.
[2024-09-18; Reflector poll]
Set status to Tentatively Ready after seven votes in favour during reflector poll.
Proposed resolution:
This wording is relative to N4988.
Modify 20.3.1.3.5 [unique.ptr.single.observers] as indicated:
constexpr add_lvalue_reference_t<T> operator*() const noexcept(noexcept(*declval<pointer>()));-?- Mandates:
reference_converts_from_temporary_v<add_lvalue_reference_t<T>, decltype(*declval<pointer>())>
isfalse
.-1- Preconditions:
get() != nullptr
istrue
.-2- Returns:
*get()
.