Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Good reason for third argument to std::accumulate?

Tags:

c++

c++11

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)

like image 967
MSalters Avatar asked Sep 09 '13 08:09

MSalters


1 Answers

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.

like image 97
Jan Hudec Avatar answered Sep 28 '22 10:09

Jan Hudec