Title
indirect<T> should be convertible to T&
Status
new
Section
[indirect.obs]
Submitter
Zhihao Yuan

Created on 2025-11-05.00:00:00 last changed 1 month ago

Messages

Date: 2025-11-06.16:51:20

Proposed resolution:

This wording is relative to N5014.

  1. Modify [indirect.syn], as indicated:

    namespace std {
      template<class T, class Allocator = allocator<T>>
      class indirect {
      public:
        […]
        // [indirect.obs], observers
        constexpr operator const T& () const & noexcept;
        constexpr operator T& () & noexcept;
        constexpr operator const T&& () const && noexcept;
        constexpr operator T&& () && noexcept;
        constexpr const T& operator*() const & noexcept;
        constexpr T& operator*() & noexcept;
        constexpr const T&& operator*() const && noexcept;
        constexpr T&& operator*() && noexcept;
        constexpr const_pointer operator->() const noexcept;
        constexpr pointer operator->() noexcept;
        […]
    
  2. Modify [indirect.obs], as indicated:

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

    -1- Preconditions: `*this` is not valueless.

    -2- Returns: *p.

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

    -3- Preconditions: `*this` is not valueless.

    -4- Returns: std::move(*p).

Date: 2025-11-05.00:00:00
Addresses US 77-140

In addition to serving as a better unique_ptr<T> in Pimpl, indirect<T> can also appear as a drop-in replacement for `T` when `T` may conditionally be an incomplete type. In which case, if indirect<T> is convertible to T&, certain tasks can be done without meta-programming (e.g., returning the object of type `T` or indirect<T> from a function with a return type T&).

History
Date User Action Args
2025-11-06 16:51:20adminsetmessages: + msg15565
2025-11-05 00:00:00admincreate