(For a concrete compiler/platform context take GCC 4.7 and Ubuntu 12.04 on x86_64)
Given some function f:
void f(int x, int y);
int nx = ...;
int ny = ...;
One way to iterate over every value (x,y) from (0,0) to (nx,ny) is:
for (int x = 0; x < nx; x++)
for (int y = 0; y < ny; y++)
f(x,y);
Let this compile to some generated code Q1.
We will write a function g such that:
for (auto it : g(Z))
f(it.x, it.y);
compiles to code Q2.
Is it possible to write g such that Q2 is as efficient as Q1? If yes, how? If not, what is the closest we can get?
You may change auto to auto& or auto&& if it helps.
You may also change it.x to it.x(), and it.y to it.y(), if it helps.
(Recall that the expansion of range-based for is just an iterator-like type of your choosing: C++11: The range-based for statement: "range-init" lifetime?)