fork(2) 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. };
  36.  
  37. int main(void)
  38. {
  39. shared_ptr<Clazz> pObj (new Clazz(42));
  40.  
  41. auto fp1 = &Clazz::foo;
  42. auto fp2 = &Clazz::bar;
  43.  
  44. cout << (pObj->*fp1)(58) << endl;
  45. cout << (pObj->*fp2)(132, 22) << endl;
  46.  
  47. return 0;
  48. }
  49.  
Success #stdin #stdout 0s 3276KB
stdin
Standard input is empty
stdout
100
5566