为什么在逗号分隔符上下文中将预递增的结果转换为 void?

Why cast the result of pre-incrementation to void in comma separator context?

正在查看 std::for_each_npossible implementation:

template<class InputIt, class Size, class UnaryFunction>
InputIt for_each_n(InputIt first, Size n, UnaryFunction f)
{
    for (Size i = 0; i < n; ++first, (void) ++i) {
        f(*first);
    }
    return first;
}

我注意到我们通常看到的部分 i++(或者,首选 ++i)由两个操作组成:

以逗号分隔。虽然大部分内容都说得通,但 (void) 演员阵容对我来说似乎有点令人惊讶。我所能猜测的是 可能 是一个重载的 operator , ,它采用 InputItSize 的推导类型,这会导致一些令人惊讶的一面-效果。这可能是原因吗?如果是,我们确定转换为 void 可以完全解决该问题吗?

Could that be the reason?

处理操作符逗号的邪恶重载确实是一个很好的理由。

我没有看到其他(有效)原因。

If yes, are we sure that cast to void solves that issue entirely?

是的,我们不能用 void 重载运算符逗号(也不能转换为 void)。