I saw this code in an old version of C++ Cookbook, and it mystifies me. This seems to compile, but I have no clue as to why the code would be written this way.
What does the T() mean? This is the init argument for std::accumulate -- the value to start the sum. I wrote a version where I put double() in place of T() (to "hardwire" the template), and it compiles. What does double() mean?
#include <iostream>
#include <numeric> // accumulate
#include <vector>
template<class T, class Iter_T>
void computeAccum(Iter_T first, Iter_T last, T& accum)
{
accum = std::accumulate(first, last, T()); // Why is init argument equal to T()?
}
int main()
{
std::vector<double> vd;
vd.push_back(1.11);
vd.push_back(2.22);
vd.push_back(4.44);
vd.push_back(3.33);
double sum1;
std::cout << "Here is the vector:" << std::endl;
std::vector<double> ::iterator it;
for (it = vd.begin(); it != vd.end(); ++it) {
std::cout << *it << std::endl;
}
computeAccum2(vd.begin(), vd.end(), sum1);
std::cout << "Answer: " << sum1 << std::endl;
}