Due to aliasing and in-place optimizations, it's often the case that for_each_element will be called with the same buffer passed more than once. We can avoid the loop logic for the extra buffer in that case.
In other words, something like:
for_each_element(x, [](int* x_i, const int* y_i) { *x_i = f(*y_i); }, y)
Where y happens to be equal to x, could be rewritten:
for_each_element(x, [](int* x_i) { *x_i = f(*x_i); })
This would save a little overhead in make_for_each_loops and for_each_element's implementation.
However, doing this safely and efficiently enough to be worth doing is tricky.
Due to aliasing and in-place optimizations, it's often the case that
for_each_elementwill be called with the same buffer passed more than once. We can avoid the loop logic for the extra buffer in that case.In other words, something like:
for_each_element(x, [](int* x_i, const int* y_i) { *x_i = f(*y_i); }, y)Where y happens to be equal to x, could be rewritten:
for_each_element(x, [](int* x_i) { *x_i = f(*x_i); })This would save a little overhead in
make_for_each_loopsandfor_each_element's implementation.However, doing this safely and efficiently enough to be worth doing is tricky.