11 Algorithms library [algorithms]

11.3 Non-modifying sequence operations [alg.nonmodifying]

11.3.4 For each [alg.foreach]

template <InputIterator I, Sentinel<I> S, class Proj = identity, IndirectUnaryInvocable<projected<I, Proj>> Fun> tagged_pair<tag::in(I), tag::fun(Fun)> for_each(I first, S last, Fun f, Proj proj = Proj{}); template <InputRange Rng, class Proj = identity, IndirectUnaryInvocable<projected<iterator_t<Rng>, Proj>> Fun> tagged_pair<tag::in(safe_iterator_t<Rng>), tag::fun(Fun)> for_each(Rng&& rng, Fun f, Proj proj = Proj{});

Effects: Calls invoke(f, invoke(proj, *i)) for every iterator i in the range [first,last), starting from first and proceeding to last - 1. [ Note: If the result of invoke(proj, *i) is a mutable reference, f may apply nonconstant functions. — end note ]

Returns: {last, std::move(f)}.

Complexity: Applies f and proj exactly last - first times.

Remarks: If f returns a result, the result is ignored.

Note: The requirements of this algorithm are more strict than those specified in ISO/IEC 14882:2014 §[alg.foreach]. This algorithm requires Fun to satisfy CopyConstructible, whereas the algorithm in the C++ Standard requires only MoveConstructible.  — end note ]