Why does std::transform and similar cast the 'for' loop increment to (void)?
C++C++ Problem Overview
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;
}
C++ Solutions
Solution 1 - C++
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.
Solution 2 - C++
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