fork(7) download
  1. #include <iostream>
  2. #include <type_traits>
  3. using namespace std;
  4.  
  5. template<typename... > struct Voider { using Type = void; };
  6. template<typename... TArgs> using VoidT = typename Voider<TArgs...>::Type;
  7.  
  8. template< class, class = void >
  9. struct hasGetCount : false_type { };
  10.  
  11. template< class T >
  12. struct hasGetCount<T, VoidT<decltype(T::getCount)>>
  13. : std::is_same<decltype(std::declval<T>().getCount()), int>::type { };
  14.  
  15.  
  16. struct TestTrue
  17. {
  18. int getCount() { return 0; }
  19. };
  20.  
  21. struct TestFalse
  22. {
  23.  
  24. };
  25.  
  26. int main()
  27. {
  28. static_assert(hasGetCount<TestTrue>::value == true, "");
  29. static_assert(hasGetCount<TestFalse>::value == false, "");
  30.  
  31. return 0;
  32. }
Compilation error #stdin compilation error #stdout 0s 0KB
stdin
Standard input is empty
compilation info
prog.cpp: In function ‘int main()’:
prog.cpp:28:2: error: static assertion failed: 
  static_assert(hasGetCount<TestTrue>::value == true, "");
  ^
stdout
Standard output is empty