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:

  1. 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:

  1. 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.
  • four
    Why write three different functions when there is already a template? When instantiating a function template, the name of the function changes, so functions 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? - VTT
  • one
    You have some kind of super-artificial example. In this version, you really do not need a template function. But it is worth imagining what it does, let's say, a little more - lines of 20 code - how the picture changes immediately, right? And the change of name ... It changes as it is - decorated :) Again - why do you need these different names? Is this not an XY problem ? - Harry
  • There will be no extra call if the compiler considers that it makes sense to embed a function. Most likely, he considers. - ixSci
  • The functions myfun and mysin , mycos have the same signature. myfun specializations can easily be used as a callback. In addition, the sample template should be without a typename , since it is not a type. template <float ( * libfun )(float)> - VTT
  • Hmm, it really turned out that the instantiated template has the same prototype, despite all the dances with the masking of names. And this instantiated template can be inserted into the callback call and the translator understands this. Who would have thought. Thank. A template works with and without typename typename. Visual Studio 2017 translator. - pepsicoca1

1 answer 1

You can not bother:

 auto fun(auto s1, auto s2) { return s1 + s2; } int main() { std::cout << fun(std::string("A"), "B") << std::endl; } 

You can take the link:

 auto constexpr f = fun<std::string>;