fork download
  1. #include <iostream>
  2. #include <functional>
  3. #include <memory>
  4.  
  5. template <typename T, typename V>
  6. int mao(T&& t, V&& v, decltype( (t.*v)(0), (int)0 ))
  7. {//オブジェクトとメンバーポインタの組み合わせ
  8. return 0;
  9. }
  10. template <typename T, typename V>
  11. int mao(T&& t, V&& v, decltype( (*t.*v)(0), (char)0) )
  12. {//ポインタ(生ポ/スマポ)とメンバーポインタの組み合わせ
  13. return 1;
  14. }
  15. template <typename T, typename V>
  16. int mao(T&& t, V&& v, ...)
  17. {//それ以外
  18. return -1;
  19. }
  20.  
  21. struct test {
  22. int a;
  23. test(int b) : a(b) { }
  24. int get(int b) const { return a+b; }
  25. };
  26.  
  27. int main()
  28. {
  29. test a(7);
  30. std::unique_ptr<test> p(new test(8));
  31. std::cout << mao(a, &test::get,0) << std::endl;
  32. std::cout << mao(&a, &test::get,0) << std::endl;
  33. std::cout << mao(p, &test::get,0) << std::endl;
  34. return 0;
  35. }
Success #stdin #stdout 0s 3472KB
stdin
Standard input is empty
stdout
0
1
1