1

In C++ lambda expression names are generated by compiler automatically giving (at least on VCC compiler it looks like this: lambda_cf01cxf....

Is ther any (doesn't matter if even non-portable) way to change this generated name to something more human readable?

Rostyslav Dzinko
  • 38,056
  • 5
  • 48
  • 60
  • "way to change this generated name to something more human readable?" - do you mean you want to somehow override the compiler's name-mangling decisions - or you just want a way to reverse the name mapping during debugging or even a text file printout of the mapping? – Dai Sep 07 '18 at 08:41
  • 6
    If you want a named function, use a normal function? – Some programmer dude Sep 07 '18 at 08:43
  • @Dai, the goal is to just reverse the name mapping during debugging, but, ideally the way to override mangling procedure. – Rostyslav Dzinko Sep 07 '18 at 08:45
  • @Someprogrammerdude, yeah would be great but debugging 3rd party with a lot of queued lambdas won't make it possible :( – Rostyslav Dzinko Sep 07 '18 at 08:46
  • 2
    Then please edit your question to include the *context*. Tell us *why* you want this reverse naming, what the *real* problem is. – Some programmer dude Sep 07 '18 at 08:48
  • @Someprogrammerdude The real problem is that there's a container with queued lambdas (hundreds) to be executed in a deferred manner, and they are queued from different places. The problem is to get the place the concrete lambda was enqueued from. – Rostyslav Dzinko Sep 07 '18 at 08:51
  • Visual studio ships a command line tool called `undname.exe` , to demangle c++ synbols. I don't know if it supports lambdas though. – nos Sep 07 '18 at 08:53
  • It might be a solution to define your own type (similar to `std::function` or simply wrapping one) that also stores the `__FILE__` and `__LINE__` where it was created. – molbdnilo Sep 07 '18 at 09:14
  • 1
    @RostyslavDzinko I have encountered this problem too, working with my framework that basically all worked on function objects (not even lambdas) and tracing where they came from is not immediately apparent. Ultimately, I'd recommend finding another way to label your entries. Perhaps give your container value_type pair semantics, with the first entry being some kind of identifying key or tag. Or your container values could be structs, containing function objects (they already can't be actual lambdas) as well as some diagnostic information. – Lightness Races in Orbit Sep 07 '18 at 09:36
  • If you just want human-readable names for lambdas then use clang compiler. It generates unique human-readable names for lambdas. – Hemant Sep 07 '18 at 10:33

1 Answers1

-1

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.

ixSci
  • 12,431
  • 5
  • 40
  • 71