1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47
| #include <iostream>
int add_func(int a, int b) { return a + b; }
class FunctionGenerator;
template <typename T> class DecoratedFunction; template <typename Ret, typename... Args> class DecoratedFunction<Ret(Args...)> { public: typedef Ret (*func_ptr_t)(Args...); explicit DecoratedFunction(FunctionGenerator *d, func_ptr_t func) : dev(d), func(func) {} Ret operator()(Args&&... args) { std::cout << "Call presudo func" << std::endl; return (*func)(args...); } private: func_ptr_t func; FunctionGenerator *dev; };
class FunctionGenerator { public: explicit FunctionGenerator() = default; template <typename FuncType> DecoratedFunction<FuncType> getFunc(FuncType* func) { return DecoratedFunction<FuncType>(this, func); } };
int main() { FunctionGenerator generator; auto func = generator.getFunc<decltype(add_func)>(&add_func); std::cout << func(3, 4) << std::endl;
return 0; }
|