Scroll to navigation

std::ranges::views::repeat,std::ranges::repeat_view(3) C++ Standard Libary std::ranges::views::repeat,std::ranges::repeat_view(3)

NAME

std::ranges::views::repeat,std::ranges::repeat_view - std::ranges::views::repeat,std::ranges::repeat_view

Synopsis

Defined in header <ranges> template< std::move_constructible W,

std::semiregular Bound = std::unreachable_sentinel_t > requires (std::is_object_v<W> && std::same_as<W, std::remove_cv_t<W>> && (1) (since C++23) (/*integer-like-with-usable-difference-type*/<Bound> || std::same_as<Bound, std::unreachable_sentinel_t>))

class repeat_view : public ranges::view_interface<repeat_view<W, Bound>> namespace views {

inline constexpr /* unspecified */ repeat = /* unspecified */; (2) (since C++23)

} Call signature template< class W >

requires /* see below */ (since C++23)

constexpr /* see below */ repeat( W&& value ); template< class W, class Bound >

requires /* see below */ (since C++23)

constexpr /* see below */ repeat( W&& value, Bound&& bound ); Helper concepts concept /*integer-like-with-usable-difference-type*/ =

/*is-signed-integer-like*/<T> || (3) (exposition only*)

(/*is-integer-like*/ <T> && std::weakly_incrementable<T>)

1) A range factory that generates a sequence of elements by repeatedly producing the same value. Can be either bounded or unbounded (infinite). 2) views::repeat(e) and views::repeat(e, f) are expression-equivalent to repeat_view<std::decay_t<decltype((E))>>(e) and repeat_view(e, f) respectively for any suitable subexpressions e and f. 3) Determines whether a type is integer-like and has a usable difference type.

repeat_view models random_access_range. If Bound is not std::unreachable_sentinel_t, repeat_view also models sized_range and common_range.


Customization point objects

The name views::repeat denotes a customization point object, which is a const function object of a literal semiregular class type. For exposition purposes, the cv-unqualified version of its type is denoted as __repeat_fn.

All instances of __repeat_fn are equal. The effects of invoking different instances of type __repeat_fn on the same arguments are equivalent, regardless of whether the expression denoting the instance is an lvalue or rvalue, and is const-qualified or not (however, a volatile-qualified instance is not required to be invocable). Thus, views::repeat can be copied freely and its copies can be used interchangeably.

Given a set of types Args..., if std::declval<Args>()... meet the requirements for arguments to views::repeat above, __repeat_fn models


* std::invocable<__repeat_fn, Args...>,
* std::invocable<const __repeat_fn, Args...>,
* std::invocable<__repeat_fn&, Args...>, and
* std::invocable<const __repeat_fn&, Args...>.

Otherwise, no function call operator of __repeat_fn participates in overload resolution.


Data members

Member Definition movable-box <W> value_ the repeating element of the view
(exposition-only member object*) Bound bound_ the sentinel value
(exposition-only member object*)

Member functions

constructor creates a repeat_view
(public member function) begin obtains the beginning iterator of a repeat_view
(public member function) end obtains the sentinel denoting the end of a repeat_view
(public member function) size obtains the size of a repeat_view if it is sized
(public member function)
Inherited from std::ranges::view_interface
returns whether the derived view is empty. Provided if it satisfies empty sized_range or forward_range.
(public member function of std::ranges::view_interface<D>) cbegin returns a constant iterator to the beginning of the range. (C++23) (public member function of std::ranges::view_interface<D>) cend returns a sentinel for the constant iterator of the range. (C++23) (public member function of std::ranges::view_interface<D>)
returns whether the derived view is not empty. Provided if ranges::empty is operator bool applicable to it.
(public member function of std::ranges::view_interface<D>)
returns the first element in the derived view. Provided if it satisfies front forward_range.
(public member function of std::ranges::view_interface<D>)
returns the last element in the derived view. Provided if it satisfies back bidirectional_range and common_range.
(public member function of std::ranges::view_interface<D>)
returns the n^th element in the derived view. Provided if it satisfies operator[] random_access_range.
(public member function of std::ranges::view_interface<D>)

std::ranges::repeat_view::repeat_view

repeat_view() requires std::default_initializable<W> = default; (1) (since C++23) constexpr explicit repeat_view( const W& value, Bound bound = Bound() ); (2) (since C++23) constexpr explicit repeat_view( W&& value, Bound bound = Bound() ); (3) (since C++23) template < class... WArgs, class... BoundArgs >

requires std::constructible_from<W, WArgs...> && std::constructible_from<Bound, BoundArgs...> (4) (since C++23) constexpr explicit repeat( std::piecewise_construct_t, std::tuple<WArgs...> value_args,

std::tuple<BoundArgs...> bound_args = std::tuple<>{} );

1) Default-initializes value_ and value-initializes bound_ . 2) Initializes value_ with value and initializes bound_ with bound. If Bound is not std::unreachable_sentinel_t and bool(bound >= 0) is false, the behavior is undefined. 3) Initializes value_ with std::move(value) and initializes bound_ with bound. If Bound is not std::unreachable_sentinel_t and bool(bound >= 0) is false, the behavior is undefined. 4) Initializes value_ with std::make_from_tuple<T>(std::move(value_args)) and bound_ with std::make_from_tuple<Bound>(std::move(bound_args)). If Bound is not std::unreachable_sentinel_t and bool(bound >= 0) is false, the behavior is undefined.

Parameters

value - the value to be repeatedly produced bound - the bound value_args - the tuple containing the initializers of value_ bound_args - the tuple containing the initializers of bound_

std::ranges::repeat_view::begin

constexpr /*iterator*/ begin() const; (since C++23)

Returns iterator (std::addressof(*value_ )).

std::ranges::repeat_view::end

constexpr /*iterator*/ end() const (1) (since C++23) requires (!std::same_as<Bound, std::unreachable_sentinel_t>); constexpr std::unreachable_sentinel_t end() const; (2) (since C++23)

1) Returns iterator (std::addressof(*value_ ), bound_ ). 2) Returns std::unreachable_sentinel.

std::ranges::repeat_view::size

constexpr auto size() const (since C++23) requires (!std::same_as<Bound, std::unreachable_sentinel_t>);

Returns to-unsigned-like (bound_ ).


Deduction guides

template< class W, class Bound = std::unreachable_sentinel_t > (since C++23) repeat_view( W, Bound = Bound() ) -> repeat_view<W, Bound>;


Nested classes

iterator the iterator type
(exposition-only member class*)

Notes


Feature-test macro Value Std Feature __cpp_lib_ranges_repeat 202207L (C++23) std::ranges::repeat_view

Example

// Run this code

#include <iostream> #include <ranges> #include <string_view> using namespace std::literals;

int main() {
// bounded overload
for (auto s : std::views::repeat("C++"sv, 3))
std::cout << s << ' ';
std::cout << '\n';


// unbounded overload
for (auto s : std::views::repeat("I know that you know that"sv)
| std::views::take(3))
std::cout << s << ' ';
std::cout << "...\n"; }

Output:

C++ C++ C++ I know that you know that I know that you know that I know that you know that ...


Defect reports

The following behavior-changing defect reports were applied retroactively to previously published C++ standards.


DR Applied to Behavior as published Correct behavior LWG 4053 C++20 unary calls to views::repeat did not decay the decay the argument
argument LWG 4054 C++20 calling views::repeat with a repeat_view creates a nested
did not create a nested repeat_view repeat_view

See also

ranges::iota_view a view consisting of a sequence generated by repeatedly incrementing an views::iota initial value (C++20) (class template) (customization point object)

2026.11.10 http://cppreference.com