#include template class Function; template class Function { private: class callable_base { public: virtual Ret call(Args...) = 0; virtual ~callable_base() {} //virtual ~callable_base() = default; // melyik számít szebbnek? van bármi különbség? }; template class callable : public callable_base { private: Fun _fun; public: callable(Fun fun):_fun(fun) {} virtual Ret call(Args... args) override { return _fun(std::forward(args)...); } virtual ~callable() override {} }; std::unique_ptr _fun; /* kérdés, hogy ezt lehet-e szebben, vagy ez így a state-of-the-art megoldás? * lent majd látod, de úgy van megoldva, hogy overloadolva van az operator= * std::nullptr_t-re. próbáltam varázsolni ilyen std::is_null_pointer-rel, * meg hasonlókkal a callable::operator()-ben, meg a Function::operator()-ben, * de nem akarta az igazat. */ bool _is_null = false; public: Function() {} template Function& operator=(Fun fun) { _fun = std::make_unique>(fun); _is_null = false; return *this; } Function& operator=(std::nullptr_t) { _is_null = true; } Ret operator()(Args... args) const { if (_is_null) throw std::bad_function_call(); return _fun->call(std::forward(args)...); } operator bool() const { return static_cast(_fun); } };