我刚刚写了一个小帮手函数作为包装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());
}
Run Code Online (Sandbox Code Playgroud)
我可能在这里忽略了一些东西.为什么这不是现有的超载std::accumulate
?功能显得如此明显,以至于不容忽视; 有人有充分理由强制要求第三个参数.
(另请参阅了解std :: accumulate - 我理解为什么你希望能够提供初始值,我只是不明白为什么它是强制性的)
这样可以推导出模板参数.
声明是
template< class InputIt, class T >
T accumulate( InputIt first, InputIt last, T init );
Run Code Online (Sandbox Code Playgroud)
并且从中推导出返回值的唯一参数是init
.由于它不是必须的迭代器的值类型.
是的,它仍然可以默认为std::iterator_traits<InputIt>::value_type()
.委员会可能根本就没有想到这一点.
PS:你把我和汽车弄糊涂了.我认为它没有被添加,因为它在C++ 03中是不可能的,并且当C++ 11完成时被忽略,因为它看起来不需要任何改变.但它不会在这里需要; 模板参数在您在标准位置写入返回类型时声明.