fork(1) 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. template<class... ActualArgs>
  16. T operator()(ActualArgs&&... args)
  17. {
  18. return ((*_objC).*_fp)(std::forward<ActualArgs>(args)...);
  19. }
  20. };
  21.  
  22. template<class C, class T, class... Args>
  23. Proxy<C,T,Args...> operator ->* (shared_ptr<C> c, T (C::*fp)(Args...))
  24. {
  25. return Proxy<C,T,Args...>(c, fp);
  26. }
  27.  
  28. struct Clazz
  29. {
  30. int v;
  31.  
  32. Clazz(int x) : v(x) {}
  33.  
  34. int foo(int w) {return v + w;}
  35. int bar(int x, int y) {return v * x + y;}
  36. void baz(int* p) {*p = v * v;}
  37. };
  38.  
  39. int main(void)
  40. {
  41. shared_ptr<Clazz> pObj (new Clazz(42));
  42.  
  43. auto fp1 = &Clazz::foo;
  44. auto fp2 = &Clazz::bar;
  45. auto fp3 = &Clazz::baz;
  46.  
  47. int q = 22;
  48. int w;
  49. int *pw = &w;
  50.  
  51. cout << (pObj->*fp1)(58) << endl;
  52. cout << (pObj->*fp2)(132, q) << endl;
  53. (pObj->*fp3)(pw);
  54. cout << w << endl;
  55.  
  56. return 0;
  57. }
  58.  
Success #stdin #stdout 0s 3276KB
stdin
Standard input is empty
stdout
100
5566
1764