ΒΆtemplate <class R>
constexpr auto from_range(R&& r) noexcept
template <class R>
constexpr auto from_range(R&& r) noexcept
Description
Constructs an [`Iterator`]($sus::iter::Iterator) from a [`std::ranges::input_range`](https://en.cppreference.com/w/cpp/ranges/input_range). C++ ranges always operate on pointers, so the resulting `Iterator` will always iterate over references to the range's values. If the input is const, the `Iterator` will iterate over const references. To iterate over values instead, use [`Iterator::cloned`]($sus::iter::IteratorBase::cloned) or [`Iterator::copied`]($sus::iter::IteratorBase::copied). If the input was an rvalue and the input owned the values iterated by the range it produces, the references returned by iterator will be to the input container now held within the iterator. To iterator over values instead, and move those values from the iterator's references, use [`moved`]($sus::iter::IteratorOverRange::moved). If the input range did not own the values it iterates over, using [`moved`]($sus::iter::IteratorOverRange::moved) will still move from the objects being iterated over by the range and cause use-after-move with potential Undefined Behaviour later. If the input range's iterators satisfy `std::ranges::bidiectional_iterator`, then the output `Iterator` will be a `DoubleEndedIterator`. If the end type (aka `std::ranges::sentinel_t`) of the input range satisfies `std::ranges::std::sized_sentinel_for <end type, begin type>`, then the output `Iterator` will be an `ExactSizeIterator`. # Examples Iterates over references of a vector, copying and summing: ``` const auto v = std::vector <i32 >({1, 2, 3}); sus_check(sus::iter::from_range(v).copied().sum() == 1 + 2 + 3); ``` Moving out of a vector and iterating over its values, not as references. This leaves behind a vector of moved-from elements. ``` auto v = std::vector <i32 >({1, 2, 3}); sus_check(sus::iter::from_range(v).moved(unsafe_fn).sum() == 1 + 2 + 3); v.clear(); ```
Declared at: sus/iter/compat_ranges.h:78
Templates
- R
Parameters
- R&& r