fork download
  1. #include <iostream>
  2. #include <string>
  3. #include <vector>
  4. #include <set>
  5.  
  6. namespace FileSystem {
  7. struct Info {
  8. std::string path;
  9. };
  10. }
  11.  
  12. #define STATIC_ASSERT(cond, msg) \
  13. typedef char static_assert_##msg[2*(!!cond)-1];
  14.  
  15. #define CREATE_MEMBER_DETECTOR(member) \
  16. template<typename T> \
  17. class detector_of_##member { \
  18. private: \
  19.   typedef char Yes[1]; \
  20.   typedef char No[2]; \
  21.   struct Base {char member;}; \
  22.   struct Derived: T, Base {}; \
  23.   \
  24.   template<typename PointerToMember, PointerToMember> \
  25.   struct Check {}; \
  26.   \
  27.   template<typename D> \
  28.   static Yes& result(...); \
  29.   template<typename D> \
  30.   static No& result(Check<char Base::*, &D::member>*); \
  31.   \
  32. public: \
  33.   enum {value = sizeof(result<Derived>(0)) == 1}; \
  34. };
  35.  
  36. CREATE_MEMBER_DETECTOR(push_back)
  37. CREATE_MEMBER_DETECTOR(path)
  38.  
  39. typedef std::set<FileSystem::Info> X;
  40. typedef std::vector<std::string> Y;
  41. typedef std::vector<FileSystem::Info> Z;
  42. int main()
  43. {
  44. STATIC_ASSERT(detector_of_push_back<X>::value, no_push_back);
  45. STATIC_ASSERT(detector_of_push_back<Y>::value, no_push_back);
  46. STATIC_ASSERT(detector_of_path<Y::value_type>::value, no_path);
  47. STATIC_ASSERT(detector_of_path<Z::value_type>::value, no_path);
  48. }
Compilation error #stdin compilation error #stdout 0s 0KB
stdin
Standard input is empty
compilation info
prog.cpp: In function ‘int main()’:
prog.cpp:13:46: error: size of array ‘static_assert_no_push_back’ is negative
 typedef char static_assert_##msg[2*(!!cond)-1];
                                              ^
prog.cpp:44:5: note: in expansion of macro ‘STATIC_ASSERT’
     STATIC_ASSERT(detector_of_push_back<X>::value, no_push_back);
     ^
prog.cpp:13:46: error: size of array ‘static_assert_no_path’ is negative
 typedef char static_assert_##msg[2*(!!cond)-1];
                                              ^
prog.cpp:46:5: note: in expansion of macro ‘STATIC_ASSERT’
     STATIC_ASSERT(detector_of_path<Y::value_type>::value, no_path);
     ^
stdout
Standard output is empty