fork(1) download
  1. #include <iostream>
  2. using namespace std;
  3.  
  4. class unsafe_func
  5. {
  6. public:
  7. template<class F>
  8. explicit unsafe_func(F f) : pd(new detail_impl<F>(f)) {
  9. }
  10.  
  11. ~unsafe_func() {
  12. delete pd;
  13. }
  14.  
  15. template<class R, class ... Args>
  16. R call(Args ... args) const {
  17. return static_cast< detail_impl<R(*)(Args ...)>* >(pd)->f_(args...);
  18. }
  19.  
  20. private:
  21. unsafe_func(unsafe_func const &);
  22. unsafe_func& operator=(unsafe_func const &);
  23.  
  24. struct detail
  25. {
  26. virtual ~detail() {}
  27. };
  28.  
  29. template<class F>
  30. struct detail_impl : detail
  31. {
  32. F f_;
  33. detail_impl(F f) : f_(f) {
  34. }
  35. };
  36.  
  37. detail* pd;
  38. };
  39.  
  40. struct helloworld
  41. {
  42. void funcA() const {
  43. std::cout << "void helloworld::funcA()" << std::endl;
  44. }
  45.  
  46. int funcB(int, int) const {
  47. std::cout << "int helloworld::funcB(int, int)" << std::endl;
  48. return 0;
  49. }
  50. };
  51.  
  52. int main() {
  53. unsafe_func pf(&helloworld::funcA);
  54. unsafe_func pf2(&helloworld::funcB);
  55.  
  56. helloworld *p = 0;
  57.  
  58. pf.call<void>(p);
  59. pf2.call<int>(p, 0, 0);
  60.  
  61. return 0;
  62. }
Success #stdin #stdout 0s 3232KB
stdin
Standard input is empty
stdout
void helloworld::funcA()
int helloworld::funcB(int, int)