fork(1) download
  1. #include <type_traits>
  2.  
  3. using namespace std;
  4.  
  5. template <typename T>
  6. class has_CompareTo_self
  7. {
  8. typedef char one[1];
  9. typedef char two[2];
  10.  
  11. template <typename C, C> struct type_check;
  12.  
  13. template <typename C> static one& test( type_check<int (C::*)(C&), &C::CompareTo>* ) ;
  14. template <typename> static two& test(...);
  15.  
  16. public:
  17. enum { value = sizeof(test<T>(0)) == sizeof(one) };
  18. };
  19.  
  20. template<class _Ty> class Array
  21. {
  22. static_assert(has_CompareTo_self<_Ty>::value, "no appropriate CompareTo found");
  23. };
  24.  
  25. class Good
  26. {
  27. public:
  28. int CompareTo(Good& other);
  29. };
  30.  
  31. class Bad
  32. {
  33. public:
  34. long CompareTo(Bad& other);
  35. };
  36.  
  37.  
  38. int main()
  39. {
  40. Array<Good> a;
  41. Array<Bad> b;
  42. return 0;
  43. }
Compilation error #stdin compilation error #stdout 0s 3092KB
stdin
Standard input is empty
compilation info
prog.cpp: In instantiation of 'class Array<Bad>':
prog.cpp:41:13:   required from here
prog.cpp:22:2: error: static assertion failed: no appropriate CompareTo found
  static_assert(has_CompareTo_self<_Ty>::value, "no appropriate CompareTo found");
  ^
stdout
Standard output is empty