This works:
#include <functional>
template < bool (*F)( int ) > class Foo {};
bool fooFunc( int n ) { return true; }
int main( int argc, char* argv[] )
{
auto a = Foo< fooFunc >();
}
but this doesn't work, because you can't convert the lambda to a function pointer:
#include <functional>
template < bool (*F)( int ) > class Foo {};
auto barFunc = [] ( int n ) -> bool { return true; };
int main( int argc, char* argv[] )
{
auto a = Foo< barFunc >();
}
and this doesn't work, because you can't use a std::function<> as a template non-type parameter:
#include <functional>
template < std::function< bool( int ) > F > class Bar {};
auto barFunc = [] ( int n ) -> bool { return true; };
int main( int argc, char* argv[] )
{
auto b = Bar< barFunc >();
}
So how do I create a template class that is able to accept a lambda enclosure as a template non-type parameter?
template <typename T>? Is it because doing so relaxes the template parameter requirements too much for your needs?template <int N>[](int)->booltobool(*)(int)wasconstexpr, this would be easy to do.constexprinstead ofconstthis would be doable. But the standard was conservative. I cannot think of a technical reason why that conversion shouldn't beconstexpr, can anyone else?