fork(1) download
  1. #include <iostream>
  2. #include <typeinfo>
  3. #include <boost/core/demangle.hpp>
  4.  
  5. template<typename T>
  6. std::string getClassName()
  7. {
  8. return boost::core::demangle(typeid(T).name());
  9. }
  10.  
  11. struct Test
  12. {
  13. Test()
  14. {
  15. std::cout << "Created instance of " << getClassName<std::decay_t<decltype(*this)>>() << '\n';
  16. }
  17. };
  18.  
  19. template<typename Derived>
  20. struct IPrintable
  21. {
  22. static std::string getMyName()
  23. {
  24. return getClassName<Derived>();
  25. }
  26. };
  27.  
  28. struct TestPrintable : IPrintable<TestPrintable>
  29. {
  30. void sayHello() const
  31. {
  32. std::cout << "Hi, I'm " << getMyName() << '\n';
  33. }
  34. };
  35.  
  36. int main() {
  37. std::cout << getClassName<int>() << '\n';
  38. Test test;
  39. TestPrintable testPrintable;
  40. testPrintable.sayHello();
  41.  
  42. return 0;
  43. }
Success #stdin #stdout 0s 15240KB
stdin
Standard input is empty
stdout
int
Created instance of Test
Hi, I'm TestPrintable