In the implementation of std::ranges::transform
in GCC's stdlibc++, why does the for
loop iterator increment have a (void)
cast?
for (; __first1 != __last1 && __first2 != __last2;
++__first1, (void)++__first2, ++__result)
In the implementation of std::ranges::transform
in GCC's stdlibc++, why does the for
loop iterator increment have a (void)
cast?
for (; __first1 != __last1 && __first2 != __last2;
++__first1, (void)++__first2, ++__result)
This is because in C++ it is possible to overload the comma operator. The benefit of that still eludes me to this day, but that's not important right now. The important point is that it's possible.
The explicit void
cast prevents unintended side effects from an unexpected invocation of an overloaded operator here, with unwanted results. Note how a single void
cast effectively prevents the comma operator on both the left and the right side from getting invoked.