Title
unique_ptr operator*() should be noexcept
Status
c++23
Section
[unique.ptr.single.observers]
Submitter
Ville Voutilainen

Created on 2016-08-04.00:00:00 last changed 12 months ago

Messages

Date: 2021-10-14.09:56:08

Proposed resolution:

This wording is relative to N4892.

[Drafting note: since this issue is all about consistency, optional's pointer-like operators are additionally included.]

  1. Edit [unique.ptr.single.general], class template unique_ptr synopsis, as indicated:

    namespace std {
      template<class T, class D = default_delete<T>> class unique_ptr {
      public:
        […]
    
        // [unique.ptr.single.observers], observers
        add_lvalue_reference_t<T> operator*() const noexcept(see below);
        […]
      };
    }
    
  2. Edit [unique.ptr.single.observers] as indicated:

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

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

    -2- Returns: *get().

  3. Edit [optional.optional.general], class template optional synopsis, as indicated:

    namespace std {
      template<class T>
      class optional {
      public:
        […]
    
        // [optional.observe], observers
        constexpr const T* operator->() const noexcept;
        constexpr T* operator->() noexcept;
        constexpr const T& operator*() const & noexcept;
        constexpr T& operator*() & noexcept;
        constexpr T&& operator*() && noexcept;
        constexpr const T&& operator*() const && noexcept;
    
        […]
      };
    }
    
  4. Edit [optional.observe] as indicated:

    constexpr const T* operator->() const noexcept;
    constexpr T* operator->() noexcept;
    

    -1- Preconditions: *this contains a value.

    -2- Returns: val.

    -3- Throws: Nothing.

    -4- Remarks: These functions are constexpr functions.

    constexpr const T& operator*() const & noexcept;
    constexpr T& operator*() & noexcept;
    

    -5- Preconditions: *this contains a value.

    -6- Returns: *val.

    -7- Throws: Nothing.

    -8- Remarks: These functions are constexpr functions.

    constexpr T&& operator*() && noexcept;
    constexpr const T&& operator*() const && noexcept;
    

    -9- Preconditions: *this contains a value.

    -10- Effects: Equivalent to: return std::move(*val);

Date: 2021-10-14.00:00:00

[ 2021-10-14 Approved at October 2021 virtual plenary. Status changed: Voting → WP. ]

Date: 2021-06-15.00:00:00

[ 2021-06-23; Reflector poll ]

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

Date: 2021-06-19.00:00:00

[ 2021-06-19 Tim updates wording ]

Date: 2021-06-19.18:11:20

[ LEWG Kona 2017 ]

->Open: Believe these should be noexcept for consistency. We like these. We agree with the proposed resolution. operator->() already has noexcept.

Also adds optional::operator*

Alisdair points out that fancy pointers might intentionally throw from operator*, and we don't want to prohibit that.

Go forward with conditional noexcept(noexcept(*decltype())).

Previous resolution [SUPERSEDED]:

This wording is relative to N4606.

[Drafting note: since this issue is all about consistency, optional's pointer-like operators are additionally included.]

  1. In [unique.ptr.single] synopsis, edit as follows:

    add_lvalue_reference_t<T> operator*() const noexcept;
    
  2. Before [unique.ptr.single.observers]/1, edit as follows:

    add_lvalue_reference_t<T> operator*() const noexcept;
    
  3. In [optional.optional] synopsis, edit as follows:

    constexpr T const *operator->() const noexcept;
    constexpr T *operator->() noexcept;
    constexpr T const &operator*() const & noexcept;
    constexpr T &operator*() & noexcept;
    constexpr T &&operator*() && noexcept;
    constexpr const T &&operator*() const && noexcept;
    
  4. Before [optional.object.observe]/1, edit as follows:

    constexpr T const* operator->() const noexcept;
    constexpr T* operator->() noexcept;
    
  5. Before [optional.object.observe]/5, edit as follows:

    constexpr T const& operator*() const & noexcept;
    constexpr T& operator*() & noexcept;
    
  6. Before [optional.object.observe]/9, edit as follows:

    constexpr T&& operator*() && noexcept;
    constexpr const T&& operator*() const && noexcept;
    
Date: 2016-08-05.00:00:00

[ 2016-08-05 Chicago ]

Ville provides an initial proposed wording.

Date: 2016-08-05.03:58:49

[ 2016-08 — Chicago ]

Thurs PM: P3, and status to 'LEWG'

Date: 2016-08-04.00:00:00

See LWG 2337. Since we aren't removing noexcept from shared_ptr's operator*, we should consider adding noexcept to unique_ptr's operator*.

History
Date User Action Args
2023-11-22 15:47:43adminsetstatus: wp -> c++23
2021-10-14 09:56:08adminsetmessages: + msg12109
2021-10-14 09:56:08adminsetstatus: voting -> wp
2021-09-29 12:57:28adminsetstatus: ready -> voting
2021-06-23 14:16:45adminsetmessages: + msg11951
2021-06-23 14:16:45adminsetstatus: open -> ready
2021-06-19 18:11:20adminsetmessages: + msg11945
2018-06-22 06:38:21adminsetmessages: + msg9977
2018-06-22 06:38:21adminsetstatus: lewg -> open
2016-08-05 17:22:18adminsetmessages: + msg8427
2016-08-05 17:22:18adminsetmessages: + msg8426
2016-08-05 03:58:49adminsetmessages: + msg8424
2016-08-05 03:58:49adminsetstatus: new -> lewg
2016-08-04 00:00:00admincreate