Reputation: 1002
Consider the following code:
#include <iostream>
template<auto lambda>
auto execute()
{
return lambda();
}
int main()
{
auto no_capture = [](){
std::cout << "Hello World\n";
};
const int local_var = 42;
auto capture_by_value = [=](){
std::cout << "Hello World " << local_var << '\n';
};
auto capture_by_reference = [&](){
std::cout << "Hello World " << local_var << '\n';
};
execute<no_capture>();
execute<capture_by_value>();
execute<capture_by_reference>();
return 0;
}
This code compiles and executes as expected. Here's the link to compiler explorer.
I want to specialize the the templated function execute
such that it only accepts one of the three lambdas. In other words, I want to restrict the acceptable template parameters to lambdas which (1) do not capture anything, or (2) only capture variables by value, or (3) only capture variables by reference. I want to know how can I enforce these restrictions using template meta-programming.
Upvotes: -3
Views: 97