fork(2) download
  1. #include <string>
  2. #include <memory>
  3. template <class T> struct TInfo;
  4. template <class T> class MyClass;
  5.  
  6. #define TINFO(type) \
  7.   template <> struct TInfo<type> { \
  8.   static char const* getName() { \
  9.   return #type; \
  10.   } \
  11.   }; \
  12.   typedef MyClass<type> type##_Class; \
  13.   typedef std::unique_ptr<MyClass<type>> type##_UPtr;
  14.  
  15.  
  16. template <class T>
  17. class MyClass {
  18. private:
  19. std::string m_typeName;
  20.  
  21. public:
  22. MyClass(std::string objName = "ObjectName")
  23. : m_typeName(std::string(TInfo<T>::getName()) + "_" + objName)
  24. {}
  25.  
  26. std::string const& getName() {
  27. return m_typeName;
  28. }
  29. };
  30.  
  31.  
  32. //usage:
  33. #include <iostream>
  34.  
  35. TINFO(int);
  36. int main()
  37. {
  38. int_UPtr pi(new int_Class());
  39. std::cout << pi->getName() << '\n';
  40. }
Success #stdin #stdout 0s 3028KB
stdin
Standard input is empty
stdout
int_ObjectName