In your comments, it seems your goal is to determine if you are in the last element when iterating forward. This is much easier to solve than finding the last element:
template<class Range, class Iterator> bool is_last_element_of( Range const& r, Iterator&& it ) { using std::end; if (it == end(r)) return false; if (std::next(std::forward<Iterator>(it)) == end(r)) return true; return false; }
the above should work with any iterable range (including arrays, std containers, or custom containers).
We check if we are end (in this case we are not the last element, and promotion will be illegal).
If we are not end , we see that std::next us end . If so, we are the last element.
Otherwise, we do not.
This will not work on iterators that do not support multiple passes.
source share