Skip to content

Instantly share code, notes, and snippets.

@koko-u
Last active June 15, 2023 13:25
Show Gist options
  • Save koko-u/9562379 to your computer and use it in GitHub Desktop.
Save koko-u/9562379 to your computer and use it in GitHub Desktop.
demangle typeid name
#include <cxxabi.h>
#include <typeinfo>
#include <memory>
#include <iostream>
#include <string>
#include <cstdlib>
using namespace std;
struct FreeDeleter {
void operator()(char* ptr) {
free(ptr);
}
};
string demangle(char const* name) {
int status;
unique_ptr<char, FreeDeleter> demangled_name(__cxxabiv1::__cxa_demangle(name, nullptr, nullptr, &status));
switch (status) {
case 0:
return string(demangled_name.get());
case -1:
return "A memory allocation failure occurred.";
case -2:
return "name is not a valid name under the C++ ABI mangling rules.";
case -3:
return "One of the arguments is invalid.";
default:
return "Unknown error.";
}
}
int main() {
cout << demangle(typeid("hello").name()) << endl;
cout << demangle(typeid(FreeDeleter).name()) << endl;
return 0;
}
@koko-u
Copy link
Author

koko-u commented Mar 16, 2014

free() してないので後でなおす

@koko-u
Copy link
Author

koko-u commented Mar 17, 2014

直した。たぶん。

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment