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

4458. indirect<T> should be convertible to T&

Section: 20.4.1.6 [indirect.obs] Status: New Submitter: Zhihao Yuan Opened: 2025-11-05 Last modified: 2025-11-06

Priority: Not Prioritized

View all issues with New status.

Discussion:

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

Proposed resolution:

This wording is relative to N5014.

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

    namespace std {
      template<class T, class Allocator = allocator<T>>
      class indirect {
      public:
        […]
        // 20.4.1.6 [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 20.4.1.6 [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).