Skip to content

Instantly share code, notes, and snippets.

@Jacob-Tate
Created December 4, 2019 05:50
Show Gist options
  • Save Jacob-Tate/2ee8483c7c5699cb4bd51f5cfd4636e0 to your computer and use it in GitHub Desktop.
Save Jacob-Tate/2ee8483c7c5699cb4bd51f5cfd4636e0 to your computer and use it in GitHub Desktop.
/*!
* @file crtp_type_name.hpp
* @brief
* @author Jacob I. Tate
* @version 0.0.1
* @date 2019
*/
#include <string>
#include <cstdlib>
#if defined(__linux) || defined(__APPLE__)
#include <cxxabi.h>
#endif
/**
* @brief Determines the type name using rtti
*
* @tparam T The type to convert
* @return std::string The string representation of the type
*
* @warning If -no-rtti is enabled this function will @a not work
* @warning This is @a not ABI stable with the plugin system
*/
template<typename T>
std::string type_name()
{
#if defined(__linux) || defined(__APPLE__)
int status;
std::string tname = typeid(T).name();
char *demangled_name = abi::__cxa_demangle(tname.c_str(), NULL, NULL, &status);
if(status == 0) {
tname = demangled_name;
std::free(demangled_name);
}
return tname;
#else
// TODO: Make a windows implementation of this
return "";
#endif
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment