But when I try to build it, I get this error on line 24:
could not convert template argument 'lambda'
to 'void (*)(const string&) {aka void (*)(const std::basic_string<char>&)}'|
I thought the lambda expression I wrote would decay to a function pointer matching the template parameter. I can guess that the constexpr qualifier might have changed the type, but without it my compiler complains that lambda needs to be declared as constexpr...
So is there a way to pass lambda expressions as template parameters?
Oh, I did not realize it was an abuse of templates. I thought it would be nice to have the option to pass a lambda expression directly as a template parameter.
I suppose I'll stick with std::function, then. Thanks for the insight.
I thought the lambda expression I wrote would decay to a function pointer
template argument deduction doesn't attempt implicit conversions, but even if you forced it by instantiating callback<+lambda>(), it wouldn't work because non-type template argument that is a function pointer must point to a function with external linkage: in order to be a part of the type, it must be a compile-time constant. What you get when decaying a lambda into a pointer isn't one. Also, the conversion operator isn't constexpr.
Why not parametrize your callback on the type of the functor, rather than its address?