fork(2) download
  1. #include <iostream>
  2.  
  3. template<class T, T> struct proxy;
  4.  
  5. template <typename T, typename R, typename... Args, R(T::*mf)(Args...)>
  6. struct proxy<R(T::*)(Args...), mf>
  7. {
  8. proxy(T& host) : m_Host(host) {}
  9.  
  10. template <typename... CArgs>
  11. R call(CArgs&&... args)
  12. {
  13. std::cout << __PRETTY_FUNCTION__ << '\n';
  14. return (m_Host.*mf)(std::forward<CArgs>(args)...);
  15. }
  16.  
  17. private:
  18. proxy& operator=(const proxy&);
  19. T& m_Host;
  20. };
  21.  
  22. class SomeHost
  23. {
  24. public:
  25. int SomeGetter()
  26. {
  27. std::cout << __PRETTY_FUNCTION__ << '\n';
  28. return 42;
  29. }
  30.  
  31. void SomeSetter(int var)
  32. {
  33. std::cout << __PRETTY_FUNCTION__ << '\n';
  34. m_Var = var;
  35. }
  36.  
  37. private:
  38. int m_Var;
  39. };
  40.  
  41. void test()
  42. {
  43. SomeHost obj;
  44. proxy<void(SomeHost::*)(int), &SomeHost::SomeSetter> g(obj);
  45. g.call(5);
  46.  
  47. proxy<int(SomeHost::*)(), &SomeHost::SomeGetter> f(obj);
  48. f.call();
  49. }
  50.  
  51. int main()
  52. {
  53. test();
  54. return 0;
  55. }
  56.  
Success #stdin #stdout 0s 3296KB
stdin
Standard input is empty
stdout
R proxy<R (T::*)(Args ...), mf>::call(CArgs&& ...) [with CArgs = {int}; T = SomeHost; R = void; Args = {int}; R (T::* mf)(Args ...) = &SomeHost::SomeSetter]
void SomeHost::SomeSetter(int)
R proxy<R (T::*)(Args ...), mf>::call(CArgs&& ...) [with CArgs = {}; T = SomeHost; R = int; Args = {}; R (T::* mf)(Args ...) = &SomeHost::SomeGetter]
int SomeHost::SomeGetter()