I've written an answer here: https://stackoverflow.com/a/44481507/2642059 which uses accumulate
.
The functor must be binary with a signature like: Ret op(const auto& a, const auto& b)
but:
The signature does not need to have
const &
The requirement on the binary functor is that it:
Must not invalidate any iterators, including the end iterators, or modify any elements of the range involved
When the object accumulated into is itself a container, I'm a unclear as to the requirements on the functor. For example is something like this allowed?
const auto range = { 0, 1, 2, 3 };
const auto Ret = accumulate(cbegin(range), cend(range), vector<int>(), [](auto& a, const auto& b){
a.push_back(b);
return a;
});
Yes, I recognize this is just a copy, I'm not asking for a better solution I'm asking about the validity of this solution.