Base class:
#include <memory> namespace cb{ template< typename R, typename ... Args > class CallbackBase { public: typedef std::shared_ptr< CallbackBase< R, Args... > > CallbackPtr; virtual ~CallbackBase() { } virtual R Call( Args ... args) = 0; }; } // namespace cb
Derived class:
namespace cb{ template< typename R, typename ... Args > class FunctionCallback : public CallbackBase< R, Args... > { public: typedef R (*funccb)(Args...); FunctionCallback( funccb cb_ ) : CallbackBase< R, Args... >(), cb( cb_ ) { } virtual ~FunctionCallback() { } virtual R Call(Args... args) { return cb( args... ); } private: funccb cb; }; }
Function to create:
namespace cb{ template < typename R, typename ...Args > typename CallbackBase< R, Args... >::CallbackBasePtr MakeCallback( typename FunctionCallback< R, Args... >::funccb cb ) { typename CallbackBase< R, Args... >::CallbackBasePtr p( new FunctionCallback< R, Args... >( cb ) ); return p; } }
And an example:
bool Foo_1args( const int & t) { return true; } int main() { auto cbObj = cb::MakeCallback( & Foo_1args ); }
I keep getting this error:
error: no matching function for call to 'MakeCallback(bool (*)(const int&))' error: unable to deduce 'auto' from '<expression error>'
I tried to change it, but I could not figure out how to fix it.
So what's wrong? And how to fix this example?
source share