fork download
  1. #include <iostream>
  2. #include <utility> //std::forward
  3. #include <memory> //std::shared_ptr
  4. using namespace std;
  5.  
  6. template<class C, class T, class... Args>
  7. struct Proxy
  8. {
  9. shared_ptr<C> _objC;
  10. typedef T (C::*funcptr_t)(Args...);
  11. funcptr_t _fp;
  12.  
  13. Proxy(shared_ptr<C> objC, funcptr_t fp): _objC(objC), _fp(fp) {}
  14.  
  15. T operator()(Args&&... args)
  16. {
  17. return ((*_objC).*_fp)(std::forward<Args>(args)...);
  18. }
  19. };
  20.  
  21. template<class C, class T, class... Args>
  22. Proxy<C,T,Args...> operator ->* (shared_ptr<C> c, T (C::*fp)(Args...))
  23. {
  24. return Proxy<C,T,Args...>(c, fp);
  25. }
  26.  
  27. struct Clazz
  28. {
  29. int v;
  30.  
  31. Clazz(int x) : v(x) {}
  32.  
  33. int foo(int w) {return v + w;}
  34. int bar(int x, int y) {return v * x + y;}
  35. void qux(int* p) {*p = v * v;}
  36. };
  37.  
  38. int main(void)
  39. {
  40. shared_ptr<Clazz> pObj (new Clazz(42));
  41.  
  42. auto fp1 = &Clazz::foo;
  43. auto fp2 = &Clazz::bar;
  44. auto fp3 = &Clazz::qux;
  45.  
  46. int *w;
  47.  
  48. cout << (pObj->*fp1)(58) << endl;
  49. cout << (pObj->*fp2)(132, 22) << endl;
  50. (pObj->*fp3)(w);
  51. cout << w << endl;
  52.  
  53. return 0;
  54. }
  55.  
Compilation error #stdin compilation error #stdout 0s 0KB
stdin
Standard input is empty
compilation info
prog.cpp: In function 'int main()':
prog.cpp:50:16: error: cannot bind 'int*' lvalue to 'int*&&'
  (pObj->*fp3)(w);
                ^
prog.cpp:15:4: note: initializing argument 1 of 'T Proxy<C, T, Args>::operator()(Args&& ...) [with C = Clazz; T = void; Args = {int*}]'
  T operator()(Args&&... args)
    ^
stdout
Standard output is empty