Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the purpose of (void) casts in the GCC standard library implementation?

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)
like image 350
uselessgoddess Avatar asked Jun 09 '21 12:06

uselessgoddess


Video Answer


1 Answers

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.

like image 68
Sam Varshavchik Avatar answered Oct 23 '22 11:10

Sam Varshavchik