In the book "A Tour of C++" (third edition for C++20
) from Bjarne Stroustrup, section 18.5.2 packaged task, we have this piece of code :
// compute the sum of [beg:end) starting with the initial value init
double accum(vector<double>::iterator beg, vector<double>::iterator end, double init)
{
return accumulate(&*beg, &*end, init);
}
Why did Stroustrup use &*
of the iterator instead of the iterator itself here? Is there any advantage, or difference, to doing it this way?
As indicated by Eljay in comment, the fact that B.Stroustrup use a double*
here instead of a std::vector<double>::iterator
in this case seems a mistake, since he corrected that in the errata.
This syntax is undefined behavior as explained in this answer.