Title
`subrange` should provide `data()`
Status
new
Section
[range.subrange.general][range.subrange.general]
Submitter
Hewill Kang

Created on 2024-12-16.00:00:00 last changed 2 days ago

Messages

Date: 2025-02-07.22:39:43

Proposed resolution:

This wording is relative to N5001.

  1. Modify [range.subrange.general] as indicated:

    namespace std::ranges {
      […]
      template<input_or_output_iterator I, sentinel_for<I> S = I, subrange_kind K =
          sized_sentinel_for<S, I> ? subrange_kind::sized : subrange_kind::unsized>
        requires (K == subrange_kind::sized || !sized_sentinel_for<S, I>)
      class subrange : public view_interface<subrange<I, S, K>> {
        […]
        constexpr bool empty() const;
        constexpr make-unsigned-like-t<iter_difference_t<I>> size() const
          requires (K == subrange_kind::sized);
        constexpr auto data() const noexcept requires contiguous_iterator<I>;
        […]
      };
      […]
    }
    
  2. Modify [range.subrange.access] as indicated:

    constexpr make-unsigned-like-t<iter_difference_t<I>> size() const
        requires (K == subrange_kind::sized);
    

    -5- Effects:

    […]

    constexpr auto data() const noexcept requires contiguous_iterator<I>;
    

    -?- Effects: Equivalent to: return to_address(begin_);

Date: 2025-02-15.00:00:00

[ 2025-02-07; Reflector poll ]

Set priority to 4 after reflector poll.

Lots or NAD votes. "If we care about `data` being noexcept, we should add conditional noexcept to `view_interface` overloads. Seems like design."

"I don't care about noexcept (impls can strengthen it if it matters), but it's a good idea to avoid the extra iterator copy."

Date: 2024-12-16.00:00:00

Currently, only four view classes in <ranges> explicitly provide `data()` members.

Two of them are `empty_view` and `single_view`, because their `data()` is always valid and can be marked `noexcept`.

The remaining two are `ref_view` and `owning_view` with constrained `data()`, which is redundant since `data()` can always obtained from `view_interface` when the underlying range is contiguous. I suspect this is because `ranges::data` is more efficient.

However, `subrange` does not have a `data()` member, which seems worth considering because this function can always be `noexcept` given that `to_address` is always `noexcept`.

History
Date User Action Args
2025-02-07 22:39:43adminsetmessages: + msg14618
2024-12-21 12:45:38adminsetmessages: + msg14525
2024-12-16 00:00:00admincreate