Last active
June 15, 2023 13:25
-
-
Save koko-u/9562379 to your computer and use it in GitHub Desktop.
demangle typeid name
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#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; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
free() してないので後でなおす