Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the reason for using &*beg instead of beg when beg is an iterator?

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?

like image 310
kingsjester Avatar asked Nov 18 '25 17:11

kingsjester


1 Answers

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.

like image 58
kingsjester Avatar answered Nov 20 '25 06:11

kingsjester



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!