fork(3) download
  1. #include <iostream>
  2. #include <string>
  3. //==============================
  4. // convert template type to string
  5. template<typename>
  6. struct template_to_string {
  7. static std::string value() { return "unknown"; }
  8. };
  9.  
  10. #define DEFINE_TYPE(X) \
  11.   struct X; \
  12.   template<> struct template_to_string<X> { \
  13.   static std::string value() { return #X; } \
  14.   };
  15.  
  16. //==============================
  17. template<class T>
  18. struct ShowClass
  19. {
  20. ShowClass() {
  21. std::cout << template_to_string<T>::value()
  22. << " show" << std::endl;
  23. }
  24. };
  25.  
  26. //==============================
  27. template <typename T>
  28. struct AutoShow
  29. {
  30. AutoShow() { std::cout << "AutoShow ctor" << std::endl; &our_show; }
  31.  
  32. protected:
  33. static ShowClass<T> our_show;
  34. };
  35. template <typename T>
  36. ShowClass<T> AutoShow<T>::our_show;
  37.  
  38.  
  39. //==============================
  40. DEFINE_TYPE(Test)
  41. struct Test
  42. {
  43. private:
  44. static ShowClass<Test> our_show;
  45. };
  46. ShowClass<Test> Test::our_show;
  47.  
  48. //==============================
  49. DEFINE_TYPE(TestOK)
  50. struct TestOK : public AutoShow<TestOK>
  51. {
  52. TestOK() {}
  53. };
  54.  
  55. //==============================
  56. DEFINE_TYPE(TestFail)
  57. struct TestFail : public AutoShow<TestFail>
  58. {
  59. };
  60.  
  61. //==============================
  62. int main()
  63. {
  64. return 0;
  65. }
  66.  
Success #stdin #stdout 0s 3228KB
stdin
Standard input is empty
stdout
Test show
TestOK show