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

3549. view_interface is overspecified to derive from view_base

Section: 26.5.3 [view.interface], 26.4.4 [range.view] Status: C++23 Submitter: Tim Song Opened: 2021-05-09 Last modified: 2023-11-22

Priority: 2

View all other issues in [view.interface].

View all issues with C++23 status.

Discussion:

view_interface<D> is currently specified to publicly derive from view_base. This derivation requires unnecessary padding in range adaptors like reverse_view<V>: the view_base subobject of the reverse_view is required to reside at a different address than the view_base subobject of V (assuming that V similarly opted into being a view through derivation from view_interface or view_base).

This appears to be a case of overspecification: we want public and unambiguous derivation from view_interface to make the class a view; the exact mechanism of how that opt-in is accomplished should have been left as an implementation detail.

The proposed resolution below has been implemented on top of libstdc++ master and passes its ranges testsuite, with the exception of a test that checks for the size of various range adaptors (and therefore asserts the existence of view_base-induced padding).

[2021-05-17; Reflector poll]

Priority set to 2.

[2021-05-26; Reflector poll]

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

[2021-06-07 Approved at June 2021 virtual plenary. Status changed: Voting → WP.]

Proposed resolution:

This wording is relative to N4885.

  1. Modify 26.4.4 [range.view] as indicated:

    template<class T>
      inline constexpr bool is-derived-from-view-interface = see below;   // exposition only
    template<class T>
      inline constexpr bool enable_view = derived_from<T, view_base> || is-derived-from-view-interface<T>;
    

    -?- For a type T, is-derived-from-view-interface<T> is true if and only if T has exactly one public base class view_interface<U> for some type U and T has no base classes of type view_interface<V> for any other type V.

    -5- Remarks: Pursuant to 16.4.5.2.1 [namespace.std], users may specialize enable_view to true for cv-unqualified program-defined types which model view, and false for types which do not. Such specializations shall be usable in constant expressions (7.7 [expr.const]) and have type const bool.

  2. Modify 26.5.3.1 [view.interface.general] as indicated:

    namespace std::ranges {
      template<class D>
        requires is_class_v<D> && same_as<D, remove_cv_t<D>>
      class view_interface : public view_base {
    
      […]
    
      };
    }