I can write a function template:
template<typename T>
void f1(T parameter) { ... }
But in C++14, I can also create a generic lambda:
auto f2 = [](auto parameter) { ... };
Within f1
I can refer to T
directly. Within f2
, there's no T
to refer to, but I can get the same effect using decltype
:
auto f2 = [](auto parameter)
{
using T = decltype(param);
...
};
An advantage of the generic lambda is that I can perfect-forward it. I can't do that with the function template:
template<typename T>
void fwdToG(T&& param) { g(std::forward<T>(param)); }
fwdToG(f1); // error!
fwdToG(f2); // okay
Are there situations where using a function template would be better than using a generic lambda?