fork(9) download
  1. #include <iostream>
  2. #include <utility>
  3.  
  4.  
  5. using namespace std;
  6.  
  7. // fix for gcc:
  8. // see http://stackoverflow.com/a/25833474/3087952
  9.  
  10. //template< class ... > using void_t = void;
  11.  
  12. template< class ... >
  13. struct make_void {
  14. typedef void type;
  15. };
  16.  
  17. template< class ... T >
  18. using void_t = typename make_void< T... >::type;
  19.  
  20. template< class , class = void >
  21. struct has_member : false_type
  22. { };
  23.  
  24. // specialized as has_member< T , void > or discarded (sfinae)
  25. template< class T >
  26. struct has_member< T , void_t< decltype( T::member ) > > : true_type
  27. { };
  28.  
  29. class A {
  30. public:
  31. int member;
  32. };
  33.  
  34. class B {
  35. };
  36.  
  37. int main() {
  38.  
  39. cout << "A has 'member' = " << boolalpha << has_member< A >::value << endl;
  40. cout << "B has 'member' = " << boolalpha << has_member< B >::value << endl;
  41.  
  42.  
  43. return 0;
  44. }
Success #stdin #stdout 0s 3296KB
stdin
Standard input is empty
stdout
A has 'member' = true
B has 'member' = false