Title
view_interface is overspecified to derive from view_base
Status
c++23
Section
[view.interface][range.view]
Submitter
Tim Song

Created on 2021-05-09.00:00:00 last changed 12 months ago

Messages

Date: 2021-06-07.16:58:04

Proposed resolution:

This wording is relative to N4885.

  1. Modify [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 [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 ([expr.const]) and have type const bool.

  2. Modify [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 {
    
      […]
    
      };
    }
    
Date: 2021-06-07.00:00:00

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

Date: 2021-05-15.00:00:00

[ 2021-05-26; Reflector poll ]

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

Date: 2021-05-15.00:00:00

[ 2021-05-17; Reflector poll ]

Priority set to 2.

Date: 2021-05-09.00:00:00

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

History
Date User Action Args
2023-11-22 15:47:43adminsetstatus: wp -> c++23
2021-06-07 16:58:04adminsetmessages: + msg11912
2021-06-07 16:58:04adminsetstatus: voting -> wp
2021-05-26 21:11:22adminsetstatus: ready -> voting
2021-05-26 10:53:33adminsetmessages: + msg11857
2021-05-26 10:53:33adminsetstatus: new -> ready
2021-05-17 12:23:56adminsetmessages: + msg11805
2021-05-09 21:46:47adminsetmessages: + msg11793
2021-05-09 00:00:00admincreate