Function Subspace :: sus :: iter :: from_range

template <class R>
auto from_range(R&& r) -> auto

Constructs an Iterator from a std::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 or Iterator::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. If the input range did not own the values it iterates over, using 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.


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);