How to unmangle mangled names of C++ lambdas?

你离开我真会死。 提交于 2019-11-30 11:27:50

You can use GCC's special abi::__cxa_demangle function:

#include <memory>
#include <cstdlib>
#include <cxxabi.h>
#include <iostream>

// delete malloc'd memory
struct malloc_deleter
{
    void operator()(void* p) const { std::free(p); }
};

// custom smart pointer for c-style strings allocated with std::malloc
using cstring_uptr = std::unique_ptr<char, malloc_deleter>;

int main()
{
    // special function to de-mangle names
    int error;
    cstring_uptr name(abi::__cxa_demangle(typeid([]{}).name(), 0, 0, &error));

    if(!error)
        std::cout << name.get() << '\n';
    else if(error == -1)
        std::cerr << "memory allocation failed" << '\n';
    else if(error == -2)
        std::cerr << "not a valid mangled name" << '\n';
    else if(error == -3)
        std::cerr << "bad argument" << '\n';
}

Output:

main::{lambda()#1}

According to The Documentation this function returns a c-style zero-terminated string allocated using std::malloc which the caller needs to free using std::free. This example uses a smart pointer to free the returned string automatically at the end of the scope.

Using c++filt version 070207 20070207:

$ c++filt -n Z4mainEUlvE_
main::'lambda'()

Although as the commenters have suggested, these names aren't always entirely helpful.

marcinj

If you don't need it inside your code, and its only for fun then use an online tool like http://d.fuqu.jp/c++filtjs/ which for Z4mainEUlvE_ it returns main::{lambda()#1}.

Other tools can be found under this Stack Overflow question.

You could try using boost::core::demangle but I don't know if your results will be any different.

For example

#include <boost/core/demangle.hpp>
#include <iostream>

int main () {
  std::cout  << boost::core::demangle (typeid ([](){}).name ()) << std::endl;
}
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!