Why does std::transform and similar cast the 'for' loop increment to (void)?

What is the purpose of (void) ++__result in the code below?

Implementation for std::transform:

// std::transform
template <class _InputIterator, class _OutputIterator, class _UnaryOperation>
inline _LIBCPP_INLINE_VISIBILITY
_OutputIterator
transform(_InputIterator __first, _InputIterator __last, _OutputIterator __result, _UnaryOperation __op)
{
for (; __first != __last; ++__first, (void) ++__result)
*__result = __op(*__first);
return __result;
}
2342 次浏览

It is possible to overload operator,. Casting either operand to void prevents any overloaded operator from being called, since overloaded operators cannot take void parameters.

It avoids call of overloaded operator, if there is any. Because the type void can't be an argument of a function (operator).

Another approach would be inserting void() in the middle:

++__first, void(), ++__result