You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

type_name.h 774B

1234567891011121314151617181920212223242526272829303132333435363738
  1. #pragma once
  2. #include <type_traits>
  3. #include <typeinfo>
  4. #ifndef _MSC_VER
  5. # include <cxxabi.h>
  6. #endif
  7. #include <memory>
  8. #include <string>
  9. #include <cstdlib>
  10. template <class T>
  11. std::string
  12. type_name()
  13. {
  14. typedef typename std::remove_reference<T>::type TR;
  15. std::unique_ptr<char, void(*)(void*)> own
  16. (
  17. #ifndef _MSC_VER
  18. abi::__cxa_demangle(typeid(TR).name(), nullptr, nullptr, nullptr),
  19. #else
  20. nullptr,
  21. #endif
  22. std::free
  23. );
  24. std::string r = own != nullptr ? own.get() : typeid(TR).name();
  25. if (std::is_const<TR>::value)
  26. r += " const";
  27. if (std::is_volatile<TR>::value)
  28. r += " volatile";
  29. if (std::is_lvalue_reference<T>::value)
  30. r += "&";
  31. else if (std::is_rvalue_reference<T>::value)
  32. r += "&&";
  33. return r;
  34. }
  35. #define TYPE_NAME(p) (type_name<decltype(p)>())