Last active
February 15, 2018 09:26
-
-
Save PaulChana/15a70221728f0e10c456667d9712454a to your computer and use it in GitHub Desktop.
[Demangle C++ types] Demangle #C++
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 <memory> | |
| #include <string> | |
| #include <cxxabi.h> | |
| class Demangler | |
| { | |
| public: | |
| template <class T> | |
| static std::string compute_object_name (T& obj) | |
| { | |
| const std::string mangled_name = typeid (obj).name (); | |
| int status = -1; | |
| std::unique_ptr<char, void(*)(void*)> res | |
| { | |
| abi::__cxa_demangle (mangled_name.c_str(), | |
| nullptr, | |
| nullptr, | |
| &status), | |
| std::free | |
| }; | |
| if (status == 0) | |
| return res.get(); | |
| return mangled_name; | |
| } | |
| }; | |
| /* | |
| Example usage: | |
| class Foo | |
| { | |
| public: | |
| Foo() = default; | |
| ~Foo() = default; | |
| void print_name() const { std::cout << Demangler::compute_object_name (*this) << std::endl; } | |
| }; | |
| */ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment