One version of my code works. Another (which I would have thought was preferable) fails to compile. If I declare the simple pair of functions:
template<class T>
void pass_function(T (*func))
{
cout << func() << endl;
}
double func_to_pass()
{
return(0);
}
I can call
pass_function(&func_to_pass);
and everything works as expected. I know that it is "figuring out" that the template is standing for a double here even though I haven't told it that it is a double in this call.
But, if instead I call
pass_function<double>(&func_to_pass);
I, naively, would think this would be better since I am trying to tell it that the function passed as the argument will return a double. But I get the error:
error: no matching function for call to ‘pass_function(double (*)())’
So, clearly I am misunderstanding something about the syntax of using templates.
Change the definition like
template<class T>
void pass_function(T (*func)())
{
cout << func() << endl;
}
Otherwise when you explicitly specify the template argument the function is specialized like
void pass_function( double * );
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.