Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>This doesn't really answer your question, because a callback has to be a template, unless you want to pass a parameter through <code>void*</code> (which is totally crazy idea in my opinion).</p> <p>I asked a similar question : <a href="https://stackoverflow.com/questions/5656691/what-is-wrong-with-this-variadic-templates-example">What is wrong with this variadic templates example?</a> </p> <p>One of answers gave a complete solution : </p> <pre><code>#include &lt;memory&gt; template&lt; typename R, typename ... Args &gt; class CallbackBase { public: typedef std::shared_ptr&lt; CallbackBase&lt; R, Args... &gt; &gt; CallbackPtr; virtual ~CallbackBase() { } virtual R Call( Args ... args) = 0; }; template&lt; typename R, typename ... Args &gt; class FunctionCallback : public CallbackBase&lt; R, Args... &gt; { public: typedef R (*funccb)(Args...); FunctionCallback( funccb cb_ ) : CallbackBase&lt; R, Args... &gt;(), cb( cb_ ) { } virtual ~FunctionCallback() { } virtual R Call(Args... args) { return cb( args... ); } private: funccb cb; }; template &lt; typename R, typename ...Args &gt; typename CallbackBase&lt; R, Args... &gt;::CallbackPtr MakeCallback( R (*cb)(Args...) ) { typename CallbackBase&lt; R, Args... &gt;::CallbackPtr p( new FunctionCallback&lt; R, Args... &gt;( cb ) ); return p; } bool Foo_1args( const int &amp; t) { return true; } int main() { auto cbObj = MakeCallback( &amp; Foo_1args ); } </code></pre> <p>Hope it helps. If you hate templates, you can always use a typedef.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload