fork download
  1.  
  2. #include <iostream>
  3. using namespace std;
  4.  
  5. #define DECLARE_SELF(Type) \
  6.   typedef Type TySelf; /**< @brief type of this class */ \
  7.   /** checks the consistency of TySelf type (calling it has no effect) */ \
  8.   void self_check() \
  9.   { \
  10.   static_assert(std::is_same<decltype(*((TySelf*)(0))), \
  11.   decltype(*this)>::value, "TySelf is not what it should be"); \
  12.   } \
  13.   enum { static_self_check_token = __LINE__ }; \
  14.   static_assert(int(static_self_check_token) == \
  15.   int(TySelf::static_self_check_token), \
  16.   "TySelf is not what it should be")
  17.  
  18. struct ABC {
  19. DECLARE_SELF(ABC);
  20. };
  21.  
  22. struct XYZ {
  23. DECLARE_SELF(ABC); // fails
  24. //DECLARE_SELF(XYZ); // runs
  25. };
  26.  
  27. template <class MyArg>
  28. struct XYZt {
  29. DECLARE_SELF(ABC); // fails, if XYZt is instantiated
  30. //DECLARE_SELF(XYZt); // runs
  31. };
  32.  
  33.  
  34. int main() {
  35. XYZt<int> instantiate_me;
  36.  
  37. return 0;
  38. }
Compilation error #stdin compilation error #stdout 0s 3292KB
stdin
Standard input is empty
compilation info
prog.cpp:14:9: error: static assertion failed: TySelf is not what it should be
         static_assert(int(static_self_check_token) == \
         ^
prog.cpp:23:9: note: in expansion of macro ‘DECLARE_SELF’
         DECLARE_SELF(ABC); // fails
         ^
prog.cpp: In member function ‘void XYZ::self_check()’:
prog.cpp:10:13: error: static assertion failed: TySelf is not what it should be
             static_assert(std::is_same<decltype(*((TySelf*)(0))), \
             ^
prog.cpp:23:9: note: in expansion of macro ‘DECLARE_SELF’
         DECLARE_SELF(ABC); // fails
         ^
prog.cpp: In instantiation of ‘struct XYZt<int>’:
prog.cpp:35:12:   required from here
prog.cpp:14:9: error: static assertion failed: TySelf is not what it should be
         static_assert(int(static_self_check_token) == \
         ^
prog.cpp:29:9: note: in expansion of macro ‘DECLARE_SELF’
         DECLARE_SELF(ABC); // fails, if XYZt is instantiated
         ^
prog.cpp: In function ‘int main()’:
prog.cpp:35:12: warning: unused variable ‘instantiate_me’ [-Wunused-variable]
  XYZt<int> instantiate_me;
            ^
stdout
Standard output is empty