How would I go about testing if a lambda is stateless, that is, if it captures anything or not? My guess would be using overload resolution with a function pointer overload, or
As per the Standard, if a lambda doesn't capture any variable, then it is implicitly convertible to function pointer.
Based on that, I came up with is_stateless<>
meta-function which tells you whether a lambda is stateless or not.
#include
template
struct helper : helper
{};
template
struct helper
{
static const bool value = std::is_convertible::value;
};
template
struct is_stateless
{
static const bool value = helper::value;
};
And here is the test code:
int main()
{
int a;
auto l1 = [a](){ return 1; };
auto l2 = [](){ return 2; };
auto l3 = [&a](){ return 2; };
std::cout<::value<< "\n";
std::cout<::value<< "\n";
std::cout<::value<< "\n";
}
Output:
false
true
false
Online Demo.