[range.utility.conv.to]

# 25 Ranges library [[ranges]](./#ranges)

## 25.5 Range utilities [[range.utility]](range.utility#conv.to)

### 25.5.7 Range conversions [[range.utility.conv]](range.utility.conv#to)

#### 25.5.7.2 ranges​::​to [range.utility.conv.to]

[ð](#lib:to,ranges)

`template<class C, [input_range](range.refinements#concept:input_range "25.4.6 Other range refinements [range.refinements]") R, class... Args> requires (![view](range.view#concept:view "25.4.5 Views [range.view]")<C>)
  constexpr C to(R&& r, Args&&... args);
`

[1](#1)

[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/ranges.tex#L2415)

*Mandates*: C is a cv-unqualified class type[.](#1.sentence-1)

[2](#2)

[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/ranges.tex#L2419)

*Returns*: An object of type C constructed from the elements of r in the following manner:

- [(2.1)](#2.1)

  If C does not satisfy [input_range](range.refinements#concept:input_range "25.4.6 Other range refinements [range.refinements]") or[convertible_to](concept.convertible#concept:convertible_to "18.4.4 Concept convertible_­to [concept.convertible]")<range_reference_t<R>, range_value_t<C>> is true:
  * [(2.1.1)](#2.1.1)

If [constructible_from](concept.constructible#concept:constructible_from "18.4.11 Concept constructible_­from [concept.constructible]")<C, R, Args...> is true:C(std::forward<R>(r), std::forward<Args>(args)...)

  * [(2.1.2)](#2.1.2)

Otherwise, if[constructible_from](concept.constructible#concept:constructible_from "18.4.11 Concept constructible_­from [concept.constructible]")<C, from_range_t, R, Args...> is true:C(from_range, std::forward<R>(r), std::forward<Args>(args)...)

  * [(2.1.3)](#2.1.3)

Otherwise, if
    +
          [(2.1.3.1)](#2.1.3.1)
[common_range](range.refinements#concept:common_range "25.4.6 Other range refinements [range.refinements]")<R> is true,

    +
          [(2.1.3.2)](#2.1.3.2)
the [*qualified-id*](expr.prim.id.qual#nt:qualified-id "7.5.5.3 Qualified names [expr.prim.id.qual]")iterator_traits<iterator_t<R>>​::​iterator_category is valid and denotes a type that models[derived_from](concept.derived#concept:derived_from "18.4.3 Concept derived_­from [concept.derived]")<input_iterator_tag>, and

    +
          [(2.1.3.3)](#2.1.3.3)
[constructible_from](concept.constructible#concept:constructible_from "18.4.11 Concept constructible_­from [concept.constructible]")<C, iterator_t<R>, sentinel_t<R>, Args...> is true:

C(ranges::begin(r), ranges::end(r), std::forward<Args>(args)...)

  * [(2.1.4)](#2.1.4)

Otherwise, if
    +
          [(2.1.4.1)](#2.1.4.1)
[constructible_from](concept.constructible#concept:constructible_from "18.4.11 Concept constructible_­from [concept.constructible]")<C, Args...> is true, and

    +
          [(2.1.4.2)](#2.1.4.2)
*container-appendable*<C, range_reference_t<R>> is true:

C c(std::forward<Args>(args)...);if constexpr ([approximately_sized_range](range.approximately.sized#concept:approximately_sized_range "25.4.3 Approximately sized ranges [range.approximately.sized]")<R> && *reservable-container*<C>) c.reserve(static_cast<range_size_t<C>>(ranges::reserve_hint(r)));
ranges::for_each(r, *container-append*(c));

  * [(2.1.5)](#2.1.5)

Otherwise, the program is ill-formed[.](#2.1.sentence-1)

- [(2.2)](#2.2)

  Otherwise,
if [input_range](range.refinements#concept:input_range "25.4.6 Other range refinements [range.refinements]")<range_reference_t<R>> is true:to<C>(ref_view(r) | views::transform([](auto&& elem) {return to<range_value_t<C>>(std::forward<decltype(elem)>(elem));}), std::forward<Args>(args)...);

- [(2.3)](#2.3)

  Otherwise, the program is ill-formed[.](#2.3.sentence-1)

[ð](#lib:to,ranges_)

`template<template<class...> class C, [input_range](range.refinements#concept:input_range "25.4.6 Other range refinements [range.refinements]") R, class... Args>
  constexpr auto to(R&& r, Args&&... args);
`

[3](#3)

[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/ranges.tex#L2497)

Let *input-iterator* be an exposition-only type:struct *input-iterator* { // *exposition only*using iterator_category = input_iterator_tag; using value_type = range_value_t<R>; using difference_type = ptrdiff_t; using pointer = add_pointer_t<range_reference_t<R>>; using reference = range_reference_t<R>;
 reference operator*() const;
 pointer operator->() const; *input-iterator*& operator++(); *input-iterator* operator++(int); bool operator==(const *input-iterator*&) const;};

[*Note [1](#note-1)*:

*input-iterator* meets
the syntactic requirements of *Cpp17InputIterator*[.](#3.sentence-1)

â *end note*]

[4](#4)

[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/ranges.tex#L2518)

Let *DEDUCE_EXPR* be defined as follows:

- [(4.1)](#4.1)

C(declval<R>(), declval<Args>()...) if that is a valid expression,

- [(4.2)](#4.2)

otherwise, C(from_range, declval<R>(), declval<Args>()...) if that is a valid expression,

- [(4.3)](#4.3)

otherwise,C(declval<*input-iterator*>(), declval<*input-iterator*>(), declval<Args>()...) if that is a valid expression,

- [(4.4)](#4.4)

otherwise, the program is ill-formed[.](#4.sentence-1)

[5](#5)

[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/ranges.tex#L2536)

*Returns*: to<decltype(*DEDUCE_EXPR*)>(std​::​forward<R>(r), std​::​forward<Args>(args)...)[.](#5.sentence-1)
