Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

c++

People also ask

What does std :: transform do?

std::transform. std::transform applies the given function to a range and stores the result in another range, keeping the original elements order and beginning at d_first . 1) The unary operation unary_op is applied to the range defined by [first1, last1) .

Can I static cast from void *?

static_cast is commonly used to cast between numerical types and covert void* to pointers pointing to a variable of a certain type. The user should pay attention to the range of the numerical type.

Can we increment void * pointer?

You cannot perform arithmetic on a void pointer because pointer arithmetic is defined in terms of the size of the pointed-to object.

Is std :: transform faster?

I timed the difference between both implementations using google benchmark and came to the conclusion that the loop is about 5 times faster than using std::transform.


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