ge ` concept is a refinement of [ ` range `
](/w/cpp/ranges/range "cpp/ranges/range") for which ` ranges::begin ` returns
a [ constant iterator ](/w/cpp/iterator#Iterator_categories "cpp/iterator") .

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.

###  [ [ edit
](/mwiki/index.php?title=cpp/ranges/constant_range&action=edit&section=1 "Edit
section: Example") ]  Example

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](http://en.cppreference.com/w/cpp/ranges/range) R>
    void takes_any_range1(R&& r)
    {
        takes_any_range1(std::[views::as_const](http://en.cppreference.com/w/cpp/ranges/as_const_view)([std::forward](http://en.cppreference.com/w/cpp/utility/forward)<R>(r)));
    }
     
    // 2) one function template that shadows its parameter
    template<std::[ranges::range](http://en.cppreference.com/w/cpp/ranges/range) R>
    void takes_any_range2(R&& _r)
    {
        auto r = std::[views::as_const](http://en.cppreference.com/w/cpp/ranges/as_const_view)([std::forward](http://en.cppreference.com/w/cpp/utility/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](http://en.cppreference.com/w/cpp/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](http://en.cppreference.com/w/cpp/ranges/as_const_view)([std::forward](http://en.cppreference.com/w/cpp/utility/forward)<R>(r)));
    }
     
    static_assert
    (
            std::ranges::constant_range<const [std::vector](http://en.cppreference.com/w/cpp/container/vector)<int>> and
        not std::ranges::constant_range<[std::vector](http://en.cppreference.com/w/cpp/container/vector)<int>> and
            std::ranges::constant_range<[std::string_view](http://en.cppreference.com/w/cpp/string/basic_string_view)> and
        not std::ranges::constant_range<[std::span](http://en.cppreference.com/w/cpp/container/span)<int>> and
            std::ranges::constant_range<[std::span](http://en.cppreference.com/w/cpp/container/span)<const int>> and
        not std::ranges::constant_range<const [std::span](http://en.cppreference.com/w/cpp/container/span)<int>>
    );
     
    int main() {}

Retrieved from " [
https://en.cppreference.com/mwiki/index.php?title=cpp/ranges/constant_range&oldid=155671
](https://en.cppreference.com/mwiki/index.php?title=cpp/ranges/constant_range&oldid=155671)
"

#####  Navigation

  * [ Support us ](http://www.cppreference.com/support)
  * [ Recent changes ](/w/Special:RecentChanges "A list of recent changes in the wiki \[r\]")
  * [ FAQ ](/w/Cppreference:FAQ)
  * [ Offline version ](/w/Cppreference:Archives)

#####  Toolbox

  * [ What links here ](/w/Special:WhatLinksHere/cpp/ranges/constant_range "A list of all wiki pages that link here \[j\]")
  * [ Related changes ](/w/Special:RecentChangesLinked/cpp/ranges/constant_range "Recent changes in pages linked from this page \[k\]")
  * [ Upload file ](http://upload.cppreference.com/w/Special:Upload "Upload files \[u\]")
  * [ Special pages ](/w/Special:SpecialPages "A list of all special pages \[q\]")
  * [ Printable version ](/mwiki/index.php?title=cpp/ranges/constant_range&printable=yes "Printable versio