fork(4) 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 ) = 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 ) = 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) << std::endl;
  32. std::cout << mao(&a, &test::get) << std::endl;
  33. std::cout << mao(p, &test::get) << std::endl;
  34. return 0;
  35. }
Compilation error #stdin compilation error #stdout 0s 3428KB
stdin
Standard input is empty
compilation info
prog.cpp: In function ‘int main()’:
prog.cpp:31:32: error: call of overloaded ‘mao(test&, int (test::*)(int)const)’ is ambiguous
  std::cout << mao(a, &test::get) << std::endl;
                                ^
prog.cpp:31:32: note: candidates are:
prog.cpp:6:5: note: int mao(T&&, V&&, decltype ((*mao::t.**mao::v(0), (int)(0)))) [with T = test&; V = int (test::*)(int)const; decltype ((*mao::t.**mao::v(0), (int)(0))) = int]
 int mao(T&& t, V&& v, decltype( (t.*v)(0), (int)0 ) = 0)
     ^
prog.cpp:16:5: note: int mao(T&&, V&&, ...) [with T = test&; V = int (test::*)(int)const]
 int mao(T&& t, V&& v, ...)
     ^
prog.cpp:32:33: error: call of overloaded ‘mao(test*, int (test::*)(int)const)’ is ambiguous
  std::cout << mao(&a, &test::get) << std::endl;
                                 ^
prog.cpp:32:33: note: candidates are:
prog.cpp:11:5: note: int mao(T&&, V&&, decltype ((**mao::t.**mao::v(0), (char)(0)))) [with T = test*; V = int (test::*)(int)const; decltype ((**mao::t.**mao::v(0), (char)(0))) = char]
 int mao(T&& t, V&& v, decltype( (*t.*v)(0), (char)0 ) = 0)
     ^
prog.cpp:16:5: note: int mao(T&&, V&&, ...) [with T = test*; V = int (test::*)(int)const]
 int mao(T&& t, V&& v, ...)
     ^
prog.cpp:33:32: error: call of overloaded ‘mao(std::unique_ptr<test>&, int (test::*)(int)const)’ is ambiguous
  std::cout << mao(p, &test::get) << std::endl;
                                ^
prog.cpp:33:32: note: candidates are:
prog.cpp:11:5: note: int mao(T&&, V&&, decltype ((**mao::t.**mao::v(0), (char)(0)))) [with T = std::unique_ptr<test>&; V = int (test::*)(int)const; decltype ((**mao::t.**mao::v(0), (char)(0))) = char]
 int mao(T&& t, V&& v, decltype( (*t.*v)(0), (char)0 ) = 0)
     ^
prog.cpp:16:5: note: int mao(T&&, V&&, ...) [with T = std::unique_ptr<test>&; V = int (test::*)(int)const]
 int mao(T&& t, V&& v, ...)
     ^
stdout
Standard output is empty