When I try the following code in compiler explorer with -O3 flag, the code generated is reduced down to basically 3 assembly instructions - mov, imul, mov
:
#include <functional>
template <typename T>
int o2 (T f, int x)
{
return f(x);
}
auto f2 = [](int x) -> int { return x*x; };
int x = 2;
int y = o2(f2, x);
Now, instead of templating the function, if I use a std::function
as below,
the actual call is still 3 instructions but the code for lambda f2
and o2
are still there:
#include <functional>
int o2(std::function<int(int)> f, int x)
{
return f(x);
}
auto f2 = [](int x) -> int { return x*x; };
int x = 2;
int y = o2(f2, x);
Why isn't the compiler not optimizing them away? Is there a way to force the compiler so that the code generated for these two snippets is the same?
I was advocating for a stronger checks using std::function
and replacing the template style. Sometimes the template error messages are not understandable. But this does not turn out to be equivalent or am I missing something?