Good day.
Let there is a template function (the examples did not broadcast):
template <class T> T fun(T s1, T s2){return s1+s2;} Now, to get different functions, you can do this:
char fun1(char s1, char s2){return fun<char>(s1,s2);} int fun2(int s1, int s2){return fun<int>(s1,s2);} float fun3(float s1, float s2){return fun<float>(s1,s2);} However, if I didn’t write these functions through a template, I would save one call:
char fun1(char s1, char s2){return s1+s2;} int fun2(int s1, int s2){return s1+s2;} float fun3(float s1, float s2){return s1+s2;} Question:
- How to make so that in case of a template function there is no unnecessary call? That is, syntactically, it should look like this, when instantiating a function template, the name of the function also changed. And the instantiated template received the new name fun1, fun2, fun3 together with the argument type parameter.
Thank.
UPD1:
The meticulous colleagues convinced me to clarify the problem. Indeed, the given example is simplified in comparison with life. Nevertheless, even an example shows the whole problem, although it may not be so obvious. We give an example. So, in fact, there is a function template with a template parameter - a pointer to another function (the examples did not translate).
template <typename float (*libfun)(float)> int myfun(int a1,float a2,double a3){ float res=libfun(a2); //здесь много обработки, одинаковой для всех libfun return ret; } Now we instantiate the template:
int ret1=myfun<&sin>(1,2,3); int ret2=myfun<&cos>(1,2,3); int ret3=myfun<&exp>(1,2,3); It seems to be all over the place. The problem is that I actually need non-template functions with the same prototype:
int mysin(int a1,float a2,double a3); int mycos(int a1,float a2,double a3); int myexp(int a1,float a2,double a3); At the same time, the functions mysin, mycos, myexp should be called from the third-party callback library . Therefore, they cannot be template and must have such a prototype.
You can do this (as actually done in the first example):
int mysin(int a1,float a2,double a3){return myfun<&sin>(a1,a2,a3);} int mycos(int a1,float a2,double a3){return myfun<&cos>(a1,a2,a3);} int myexp(int a1,float a2,double a3){return myfun<&exp>(a1,a2,a3);} But it turns out an extra challenge.
So again the same question:
- How to make so that in case of a template function there is no unnecessary call? That is, syntactically, it should look like this, when instantiating a function template, the name of the function also changed. And the instantiated template received the new name mysin, mycos, myexp along with the argument type parameter.
fun<char>,fun<int>, etc. are obtained. If you just need three different functions, then why the template? What makes you think that if a template function is called from an ordinary one, there really will be an extra call? - VTTmyfunandmysin,mycoshave the same signature.myfunspecializations can easily be used as a callback. In addition, the sample template should be without atypename, since it is not a type.template <float ( * libfun )(float)>- VTT