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.

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: 2024-12-21

Priority: Not Prioritized

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.

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: to_address(begin_);