4183. subrange should provide data()

Section: 25.5.4.1 [range.subrange.general], 25.5.4.1 [range.subrange.general] Status: New Submitter: Hewill Kang Opened: 2024-12-16 Last modified: 2025-02-07

Priority: 4

View all issues with New status.

Discussion:

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.

[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."

Proposed resolution:

This wording is relative to N5001.

  1. Modify 25.5.4.1 [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 25.5.4.3 [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_);