Title
unique_ptr's operator* is missing a mandate
Status
new
Section
[unique.ptr.single.observers] [unique.ptr.runtime.observers]
Submitter
Brian Bi

Created on 2023-03-27.00:00:00 last changed 12 months ago

Messages

Date: 2023-06-01.14:48:40

Proposed resolution:

This wording is relative to n4944.

  1. Add the following bullet before [unique.ptr.single.observers] paragraph 1:

    
    constexpr add_lvalue_reference_t<T> operator*() const noexcept(noexcept(*declval<pointer>()));
    

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

    -2- Returns Effects: Equivalent to: return *get();.

  2. Add the following bullet before [unique.ptr.runtime.observers] paragraph 1:

    
    constexpr T& operator[](size_t i) const;
    

    -1- Preconditions: get() != nullptr i < the number of elements in the array to which the stored pointer points.

    -2- Returns Effects: Equivalent to: return get()[i];.

Date: 2023-04-15.00:00:00

[ 2023-04-03; Jonathan provides new wording as requested by LWG ]

Date: 2023-03-15.00:00:00

[ 2023-03-28; Reflector poll ]

Set priority to 3 after reflector poll.

"It would be nice to Mandate !reference_converts_from_temporary_v<add_lvalue_reference_t<T>, decltype(*get())>." "noexcept-specifier isn't quite right, conversion from *get() to T& can throw."

This wording is relative to n4944.

  1. Add the following bullet before [unique.ptr.single.observers] paragraph 1:

    
    constexpr add_lvalue_reference_t<T> operator*() const noexcept(noexcept(*declval<pointer>()));
    

    -?- Mandates: *get() is a valid expression that is convertible to add_lvalue_reference_t<T>.

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

    -2- Returns: *get().

  2. Add the following bullet before [unique.ptr.runtime.observers] paragraph 1:

    
    constexpr T& operator[](size_t i) const;
    

    -?- Mandates: get()[i] is a valid expression that is convertible to T&.

    -1- Preconditions: i < the number of elements in the array to which the stored pointer points.

    -2- Returns: get()[i].

Date: 2023-06-01.15:13:39

The return type of std::unique_ptr<T>::operator* is std::add_lvalue_reference_t<T>, but there is no mandate stating that *get() is convertible to that type. There also does not appear to be a mandate that *get() is a valid expression; dereferenceability is not part of the Cpp17NullablePointer requirements. A similar issue appears to exist for std::unique_ptr<T[]>::operator[].

History
Date User Action Args
2023-06-01 14:48:40adminsetmessages: + msg13612
2023-04-03 12:43:16adminsetmessages: + msg13509
2023-03-27 14:46:04adminsetmessages: + msg13498
2023-03-27 00:00:00admincreate