This page is a snapshot from the LWG issues list, see the Library Active Issues List for more information and the meaning of WP status.

4148. unique_ptr::operator* should not allow dangling references

Section: 20.3.1.3.5 [unique.ptr.single.observers] Status: WP Submitter: Jonathan Wakely Opened: 2024-09-02 Last modified: 2024-11-28

Priority: Not Prioritized

View all other issues in [unique.ptr.single.observers].

View all issues with WP 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.

[Wrocław 2024-11-23; Status changed: Voting → WP.]

Proposed resolution:

This wording is relative to N4988.

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

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

    -2- Returns: *get().