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

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

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

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 Voting 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.

  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().