My question is to expanding on this: Why can lambdas be better optimized by the compiler than plain functions?
To reiterate, the conclusion is that lambdas create different specializations which compilers can trivially inline, whereas function pointers are not as easy to inline since there is a single specialization for a set of function prototypes. Considering that, would function pointer templates as-fast-as/faster lambdas?
int add(int a, int b) { return a + b; }
int sub(int a, int b) { return a - b; }
template <class F>
int operate(int a, int b, F func)
{
return func(a, b);
}
template <int func(int, int)>
int operateFuncTemplate(int a, int b)
{
return func(a, b);
}
int main()
{
// hard to inline (can't determine statically if operate's f is add or sub since its just a function pointer)
auto addWithFuncP = operate(1, 2, add);
auto subWithFuncP = operate(1, 2, sub);
// easy to inline (lambdas are unique so 2 specializations made, each easy to inline)
auto addWithLamda = operate(1, 2, [](int a, int b) { return a + b; });
auto subWithLamda = operate(1, 2, [](int a, int b) { return a - b; });
// also easy to inline? specialization means there are 2 made, instead of just 1 function definition with indirection?
auto addWithFuncT = operateFuncTemplate<add>(1, 2);
auto subWithFuncT = operateFuncTemplate<sub>(1, 2);
}
So if I could rank these on a scale of performance then:
operatorFuncTemplate
>= operate<LAMBDA>
>= operate<FUNCTIONPTR>
Are there instances where this relation could fail in non-trivial examples?