4035. single_view should provide empty

Section: 26.6.3.2 [range.single.view] Status: New Submitter: Hewill Kang Opened: 2023-12-31 Last modified: 2024-01-15 04:53:24 UTC

Priority: Not Prioritized

View all other issues in [range.single.view].

View all issues with New status.

Discussion:

Although single_view::empty can be synthesized through view_interface, it seems more worthwhile to provide a static empty for it which eliminates the need to pass in an object parameter, guarantees noexcept-ness, and is consistent with the design of empty_view (demo):

#include <ranges>
auto empty = std::views::empty<int>;
static_assert(noexcept(empty.empty()));
static_assert(noexcept(empty.size()));
auto single = std::views::single(0);
static_assert(noexcept(single.empty())); // fire
static_assert(noexcept(single.size()));

Proposed resolution:

This wording is relative to N4971.

  1. Modify 26.6.3.2 [range.single.view] as indicated:

    namespace std::ranges {
      template<move_constructible T>
        requires is_object_v<T>
      class single_view : public view_interface<single_view<T>> {
        […]
      public:
        […]
        constexpr T* begin() noexcept;
        constexpr const T* begin() const noexcept;
        constexpr T* end() noexcept;
        constexpr const T* end() const noexcept;
        static constexpr bool empty() noexcept;
        static constexpr size_t size() noexcept;
        constexpr T* data() noexcept;
        constexpr const T* data() const noexcept;
      };
      […]
    }
    
    […]
    constexpr T* end() noexcept;
    constexpr const T* end() const noexcept;
    

    -5- Effects: Equivalent to: return data() + 1;

    static constexpr bool empty() noexcept;
    

    -?- Effects: Equivalent to: return false;