std::experi...es::for_each(3)
okay so this is std::experi...es::for_each(3), straight outta the C++ Standard Libary manual β yes bestie, thatβs the manual name, typo and all, weβre keeping it π
β¨
β¨ NAME β¨
std::experimental::ranges::for_each β and the original literally repeats it: std::experimental::ranges::for_each. Double-name moment, no cap π«Ά
πΈ Synopsis πΈ
Okay so the synopsis starts with the header, then the template soup β make it fashion:
Defined in header <experimental/ranges/algorithm>
template< InputIterator I, Sentinel<I> S, class Proj =
ranges::identity,
IndirectUnaryInvocable<projected<I, Proj>> Fun >
(1) (ranges TS)
ranges::tagged_pair<tag::in(I), tag::fun(Fun)>
for_each( I first, S last, Fun f, Proj proj = Proj{} );
template< InputRange R, class Proj = ranges::identity,
IndirectUnaryInvocable<projected<ranges::iterator_t<R>,
Proj>> Fun >
(2) (ranges TS)
ranges::tagged_pair<tag::in(ranges::safe_iterator_t<R>),
tag::fun(Fun)>
for_each( R&& r, Fun f, Proj proj = Proj{} );
- Invokes the given function object
fto the result of invoking the projectionprojon dereferencing every iterator in the range[first, last)(i.e.,ranges::invoke(f, ranges::invoke(proj, *i))), in order. - Same as (1), but uses
ras the source range, as if usingranges::begin(r)asfirstandranges::end(r)aslast.
For both overloads, if the iterator type is mutable, f may modify the elements of the range through the dereferenced iterator. If f returns a result, the result is ignored.
Unlike the rest of the algorithms, for_each is not allowed to make copies of the elements in the sequence even if they are trivially copyable. Like, do not clone the elements, bestie π
Unlike std::for_each (which requires only MoveConstructible), these functions require Fun to model CopyConstructible. Thatβs the vibe check for Fun, periodt.
Notwithstanding the declarations depicted above, the actual number and order of template parameters for algorithm declarations is unspecified. Thus, if explicit template arguments are used when calling an algorithm, the program is probably non-portable β so maybe donβt do that unless youβre ready for chaos π
π Parameters π
first,lastβ the range to apply the function torβ the range to apply the function tofβ callable object to be applied to each projected element in the rangeprojβ projection to apply to the elements
π Return value π
A tagged_pair object containing the following two members:
- The first member, with the tag
tag::in, is the past-the-end iterator of the source range (that is, an iterator of typeIthat compares equal to the sentinellast). - The second member, with the tag
tag::fun, is initialized fromstd::move(f)(after all applications of the function object).
π₯ Complexity π₯
Exactly last - first applications of f and proj. Thatβs the whole budget, bestie β no extra passes, no sneaky loops.
π§ Possible implementation π§
Hereβs the possible implementation, cute and exact:
template<InputIterator I, Sentinel<I> S, class Proj =
ranges::identity,
IndirectUnaryInvocable<ranges::projected<I, Proj>> Fun>
auto for_each(I first, S last, Fun f, Proj proj = Proj{})
-> ranges::tagged_pair<tag::in(I), tag::fun(Fun)>
{
for (; first != last; ++first)
ranges::invoke(f, ranges::invoke(proj, *first));
return {std::move(first), std::move(f)};
}
π Example π
This section is incomplete Reason: no example
Not me reading a manual page with no example π iconic in its own way.
π See also π
transformβ applies a function to a range of elements (function template)range-for loop(C++11)β executes loop over rangefor_eachβ applies a function to a range of elements (function template)for_each_n(C++17) β applies a function object to the first N elements of a sequence (function template)
π Category π
- Todo no example
mwah π β source/version/date vibes, because we do not skip the footer: http://cppreference.com, 2024.06.10, page std::experi...es::for_each(3). Thatβs the whole manual page, bestie β go slay π«Άβ¨
π¦ where this page comes from
- nixpkgs unstable: stdman 2024.07.05
- opensuse-leap 16.0: stdman 2024.07.05-bp160.1.9 Β· MIT
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash-1017 on : a language model's rewrite of the original, under its terms