c++ - The perfect callback function -


goal : obtain callback function take type of parameters callback function's parameters

.h template <typename f, typename a> void delayedcallback(f&& callbackfunction, a&& args = null); 

/

.cpp void delayedcallback(f&& callbackfunction, a&& args) {   //timer function received function pointer "callback"  function   timer((std::bind(std::forward<f>(callbackfunction), std::forward<a>(args)))()) } 

/

delayedcallback(&hudexit);  void hudexit() {} 

/

error : delayedcallback(fname,float,f &&,a &&)' : not deduce template argument 'a' 

what doing wrong? i'm new of these concept in c++, more of c# programmer

edit : it's not error, i'm pretty sure it's not 1 making.

your error message doesn't match signature of delayedcallback

template <typename f, typename a> void delayedcallback(f&& callbackfunction, a&& args = null)  delayedcallback(&hudexit); 

that function signature , usage you've shown not produce error message says

error : delayedcallback(fname,float,f &&,a &&)' : not deduce template argument 'a'

but ignoring template parameter mismatches, code you've shown result in similar error. problem template parameters cannot deduced default arguments , a treated non-deduced context in example.

from n3337, §14.8.2.5/5 [temp.deduct.type]

the non-deduced contexts are:
...
— template parameter used in parameter type of function parameter has default argument being used in call argument deduction being done.

instead, should change a parameter pack. that'll allow pass 0 or more arguments delayedcallback.

template <typename f, typename... a> void delayedcallback(f&& callbackfunction, a&&... args) {   //timer function received function pointer "callback"  function   timer((std::bind(std::forward<f>(callbackfunction), std::forward<a>(args)...))())   // previous line missing semicolon @ least } 

once fix that, you'll run problem mentioned in comments. cannot split declaration , definition of function template between header , source file non-template. implement delayedcallback in header have done above.


Comments

Popular posts from this blog

Magento/PHP - Get phones on all members in a customer group -

php - .htaccess mod_rewrite for dynamic url which has domain names -

Website Login Issue developed in magento -