I am stuck on specializing a template function for a lambda :
class X
{
public:
template <typename T>
void f(T t)
{
std::cout << "awesome" << std::endl;
};
template <>
void f(double t)
{
std::cout << "trouble" << std::endl; // Works
}
template <>
void f(??? t) // what to put here?
{
std::cout << "lambda" << std::endl;
}
};
X x;
x.f(42); // prints "awesome"
x.f(1.12); // prints "trouble"
x.f([](){ std::cout << "my lazy lambda" << std::endl; }); // should print "lambda"
Casting the lambda to a std::function before passing to f and specializing for this type works, but is tedious to write. Is there a solution in C++0x?
Edit: I am totally fine with a solution, which would enable me to specialize for a callable if the last line of passing a lambda works.
you cannot do this directly: the type of the lambda is created by the compiler and is different for each lambda. You can specialize for it, but it would be for that type only (see example below). You can remove some of the tediousness though by using a small function for converting lambda -> std::function.