Is it possible to constrain the type of capture of a lambda given as parameter ?
For example, Is it possible to take only lambdas that don’t capture anything by reference ?
template <typename F>
void f(const F& lambda) // F must be a lambda that do not capture by ref
{
:::
}
MSalters notes that “non-capturing lambda’s can be converted to a pointer-to-function.” What does this mean? The lambda object will match a pointer to function parameter type.
It’s tricky to translate the lambda type to a pointer-to-function. Here is my attempt at a compliant implementation. It’s slightly hackish.
This will not accept function pointers as direct arguments, since the template parameter needs to resolve to a functor. You could make it do so by providing a specialization for
ptmf_to_pfthat accepts pointer to function types.Also, as the demo shows, it won’t accept lambdas that capture anything by value, as well as by reference. There is no way in C++ to make the restriction so specific.