std::ranges...nstant_range(3)

πŸ“¦ version 2024.07.05, in nixpkgs unstable, opensuse-leap 16.0

πŸ’– std::ranges::constant_range πŸ’–

Okay bestie, this page is from the C++ Standard Libary manual (yes, that's how the header spells it, and we love her anyway πŸ’€). The header truncates the title as std::ranges...nstant_range(3), but the concept's full name is std::ranges::constant_range, and she is lowkey iconic. This is all about a concept that checks if a range is giving read-only, constant-iterator energy. Let's get into it.

🌸 NAME 🌸

  • std::ranges::constant_range β€” std::ranges::constant_range (the NAME section is literally the same thing twice, no cap πŸ’…).

✨ Synopsis ✨

Bestie, here's the exact synopsis, and yes it's Defined in header <ranges> and since C++23:

          Defined in header <ranges>
          template< class T >

          concept constant_range =                                      (1)
       (since C++23)
              ranges::input_range<T> &&

              /*constant-iterator*/<ranges::iterator_t<T>>;
          Helper concepts
          template< class T >

          concept /*constant-iterator*/ =
              std::input_iterator<T> &&                                 (2)
       (exposition only*)

              std::same_as<std::iter_const_reference_t<T>,
          std::iter_reference_t<T>>;

1) constant_range is a refinement of range where ranges::begin returns a constant iterator. That's the whole concept, and it ate. πŸ‘‘

2) The exposition-only concept /*constant-iterator*/<T> is satisfied when the result of the indirection operation of the input iterator is its const reference type β€” which implies read-only. Read-only. Periodt. πŸ’…

πŸŽ€ Example πŸŽ€

Okay so this example is literally three mechanisms for ensuring the parameter is a constant range, plus a static_assert that is a whole vibe check. Run this code:

       // Run this code

        #include <ranges>
        #include <span>
        #include <string_view>
        #include <vector>

        // mechanisms for ensuring the parameter is a constant range
        // 1) an overload set where the mutable one defers to the constant one
        template<std::ranges::constant_range R>
        void takes_any_range1(R&& r)
        {
            // R is definitely a constant range
        }

        template<std::ranges::range R>
        void takes_any_range1(R&& r)
        {
            takes_any_range1(std::views::as_const(std::forward<R>(r)));
        }

        // 2) one function template that shadows its parameter
        template<std::ranges::range R>
        void takes_any_range2(R&& _r)
        {
            auto r = std::views::as_const(std::forward<R>(_r));

            // r is definitely a constant range
            // never use _r again
        }

        // 3) one function template that recursively invokes itself
        template<std::ranges::range R>
        void takes_any_range3(R&& r)
        {
            if constexpr (std::ranges::constant_range<R>)
            {
                // R is definitely a constant range
                // put implementation here
            }
            else
                takes_any_range3(std::views::as_const(std::forward<R>(r)));
        }

        static_assert
        (
                std::ranges::constant_range<const std::vector<int>> and
            not std::ranges::constant_range<std::vector<int>> and
                std::ranges::constant_range<std::string_view> and
            not std::ranges::constant_range<std::span<int>> and
                std::ranges::constant_range<std::span<const int>> and
            not std::ranges::constant_range<const std::span<int>>
        );

        int main() {}

The static_assert is checking const std::vector<int>, std::vector<int>, std::string_view, std::span<int>, std::span<const int>, and const std::span<int> β€” and the nots are doing their thing. int main() {} is literally the whole main, and she's serving minimalist main-character energy. πŸ¦‹


🫢 source: http://cppreference.com Β· page date/version: 2024.06.10 Β· from the C++ Standard Libary manual β€” bye bestie, go make your ranges const πŸ’–βœ¨
πŸ“¦ where this page comes from