I just wrote a small helper function as a wrapper of std::accumulate
:
template <typename FwdIter> inline
auto accumulate(FwdIter begin, FwdIter end) -> std::iterator_traits<FwdIter>::value_type
{
return std::accumulate(begin, end, std::iterator_traits<FwdIter>::value_type());
}
I'm probably overlooking something here. Why isn't this an existing overload of std::accumulate
? The function appears so obvious that it can't have been overlooked; someone had a good reason to make that third parameter mandatory.
(See also Understanding std::accumulate - I understand why you want the ability to provide an initial value, I just don't understand why it is mandatory)
So that the template arguments can be deduced.
The declaration is
template< class InputIt, class T >
T accumulate( InputIt first, InputIt last, T init );
and the only argument to deduce the return value from is init
. Because it does not have to be value type of the iterator.
Yes, it could still default to std::iterator_traits<InputIt>::value_type()
. The committee probably simply didn't think of that.
PS: You confused me with the auto. I thought it was not added because it was not possible in C++03 and overlooked when C++11 was done because it didn't look like needing any change. But it is not needed here; the template argument is declared at the time you write return type in the standard position.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With