Here is a way to create a named lambda
#include <iostream>
#include <type_traits>
using namespace std;
#define CREATE_NAMED_LAMBDA(name, lambda) \
[](auto&& fun)\
{\
using lambdaType = typename decay<decltype(fun)>::type;\
struct lambda_ ## name : lambdaType\
{ using lambdaType::operator(); };\
return lambda_ ## name{forward<decltype(fun)>(fun)};\
}(lambda)
int main()
{
auto lamb = CREATE_NAMED_LAMBDA(my, [](int i) { cout << i; });
lamb(7);
}
You can make the macro name smaller, make it empty (just passing lambda) in non-debug builds and many other improvements. There is a catch, though: this code won't help you. At least for the time being because it crashes the latest MSVC compiler. The bug report can be tracked there.