-
-
Save vgenty/e45c8fb844e36fdec29c010c088a593d to your computer and use it in GitHub Desktop.
This file contains 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
#ifndef TYPE_HPP | |
#define TYPE_HPP | |
#include <string> | |
#include <typeinfo> | |
std::string demangle(const char* name); | |
template <class T> | |
std::string type(const T& t) { | |
return demangle(typeid(t).name()); | |
} | |
#endif | |
#ifdef __GNUG__ | |
#include <cstdlib> | |
#include <memory> | |
#include <cxxabi.h> | |
std::string demangle(const char* name) { | |
int status = -4; | |
std::unique_ptr<char, void(*)(void*)> res { | |
abi::__cxa_demangle(name,nullptr,nullptr,&status), | |
std::free | |
}; | |
return ( status == 0 ) ? res.get() : name; | |
} | |
#else | |
std::string demangle(const char* name) { | |
return name; | |
} | |
#endif | |
#include <iostream> | |
struct Base { virtual ~Base() {} }; | |
struct Derived : public Base {}; | |
int main() { | |
Base* ptr_base = new Derived(); | |
std::cout << "Type of ptr_base : " << type(ptr_base) << std::endl; | |
std::cout << "Type of pointee : " << type(*ptr_base) << std::endl; | |
delete ptr_base; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment