Understanding std::accumulate

前端 未结 5 2039
轮回少年
轮回少年 2020-12-23 11:11

I want to know why std::accumulate (aka reduce) 3rd parameter is needed. For those who do not know what accumulate is, it\'s used like so:

5条回答
  •  陌清茗
    陌清茗 (楼主)
    2020-12-23 11:39

    It's indeed not needed. Our codebase has 2 and 3-argument overloads which use a T{} value.

    However, std::accumulate is pretty old; it comes from the original STL. Our codebase has fancy std::enable_if logic to distinguish between "2 iterators and initial value" and "2 iterators and reduction operator". That requires C++11. Our code also uses a trailing return type (auto accumulate(...) -> ...) to calculate the return type, another C++11 feature.

提交回复
热议问题