fork(1) download
  1. #include <iostream>
  2. using namespace std;
  3.  
  4. struct Action
  5. {
  6. template <typename T>
  7. static bool func_real(T a){return func(a, special_());}
  8.  
  9. private:
  10.  
  11. struct general_ {};
  12. struct special_ : general_ {};
  13. template<typename> struct bool_ { typedef bool type; };
  14.  
  15. template<typename S, typename bool_<decltype(std::declval<S>().Y())>::type = 0>
  16. static bool func(S a, special_) {
  17. cout<<"Y() exists"<<endl;
  18. if(a.X()){
  19. return a.Y();
  20. }
  21. return false;
  22. }
  23.  
  24. template<typename S>
  25. static bool func(S a, general_) {
  26. cout<<"Y() does not exist"<<endl;
  27. return false;
  28. }
  29. };
  30.  
  31.  
  32. struct Has{
  33. bool X(){return true;}
  34. bool Y(){return true;}
  35. };
  36.  
  37. struct Has2{
  38. bool X(){return true;}
  39. bool Y(){return false;}
  40. };
  41.  
  42. struct HasNo{
  43. bool X(){return false;}
  44. };
  45.  
  46. int main() {
  47. Has has;
  48. Has2 has2;
  49. HasNo no;
  50. cout<< Action::func_real<Has>(has) <<endl;
  51. cout<< Action::func_real<Has2>(has2) <<endl;
  52. cout<< Action::func_real<HasNo>(no) <<endl;
  53. return 0;
  54. }
Success #stdin #stdout 0s 3296KB
stdin
Standard input is empty
stdout
Y() exists
1
Y() exists
0
Y() does not exist
0