Title
unique_ptr::operator* should not allow dangling references
Status
voting
Section
[unique.ptr.single.observers]
Submitter
Jonathan Wakely

Created on 2024-09-02.00:00:00 last changed yesterday

Messages

Date: 2024-09-18.22:27:46

Proposed resolution:

This wording is relative to N4988.

  1. Modify [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>())> is `false`.

    -1- Preconditions: `get() != nullptr` is `true`.

    -2- Returns: `*get()`.

Date: 2024-09-15.00:00:00

[ 2024-09-18; Reflector poll ]

Set status to Tentatively Ready after seven votes in favour during reflector poll.

Date: 2024-09-02.00:00:00

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.

History
Date User Action Args
2024-11-19 16:09:07adminsetstatus: ready -> voting
2024-09-18 22:27:46adminsetmessages: + msg14381
2024-09-18 22:27:46adminsetstatus: new -> ready
2024-09-02 12:23:01adminsetmessages: + msg14355
2024-09-02 00:00:00admincreate