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

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionwesView Question on Stackoverflow
Solution 1 - C++user743382View Answer on Stackoverflow
Solution 2 - C++AbyxView Answer on Stackoverflow