Passing Function Pointers and Function Objects to Algorithm accumulate

Lines 43–44 call function accumulate with a pointer to function sumSquares as its last argument. Similarly, the statement in lines 51–52 calls accumulate with an object of class SumSquaresClass as the last argument. The expression SumSquaresClass<int>() creates (and calls the default constructor for) an instance of class SumSquaresClass (a function object) that’s passed to accumulate, which invokes the function operator(). Lines 51–52 could be written as two separate statements, as follows:

SumSquaresClass< int > sumSquaresObject;result = accumulate( integers.cbegin(), integers.cend(),   0, sumSquaresObject );

The first line defines an object ...

Get C++11 for Programmers, Second Edition now with O’Reilly online learning.

O’Reilly members experience live online training, plus books, videos, and digital content from 200+ publishers.