Ответ 1
Вы можете использовать специальную функцию abi::__cxa_demangle
GCC:
#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';
}
Вывод:
main::{lambda()#1}
В соответствии с Документация эта функция возвращает строку с нулевым символом c-style, выделенную с помощью std:: malloc, который вызывающий должен освободить, используя std:: free. В этом примере используется интеллектуальный указатель, чтобы автоматически освободить возвращаемую строку в конце области.