2

I have the following C++ code which compiles:

A function:

template <typename T> T funcC(int a, double b)
{
    return a + b;
}

Then in main, I do the following:

int (*anythingInt)(int,double);
double (*anythingDouble)(int,double);

anythingInt = &funcC;
anythingDouble = &funcC;

std::cout << anythingInt(12, 17.5) << std::endl;
std::cout << anythingDouble(12, 17.5) << std::endl;

As I expected, the first cout gives 29, the second one gives 29.5. My question is - how does this work? Does the compiler know to instantiate funcC with a double return type because anythingDouble uses this return type? If I do anythingInt = &funcC<int> and anythingDouble = &funcC<double>, then it yields the same behavior.

From what I read, function pointers can point to instances of template functions, not template functions themselves - like here.

I am not doing anything of it, so how does it work, and is it legitimate code?

Rohan Bari
  • 7,482
  • 3
  • 14
  • 34
  • 1
    First things first, instead of saying "template functions" the correct term is "function template" – Jason Nov 27 '22 at 14:11
  • 1
    Don't use `std::endl` unless you need the extra stuff that it does. `'\n'` ends a line. – Pete Becker Nov 27 '22 at 14:16
  • 1
    @JasonLiam *The C++ Programming Language 4th edition*, 23.2.1 Defining a Template: There are people who make semantic distinctions between the terms class template and template class. I don't; that would be too subtle: please consider those terms interchangeable. Similarly, I consider the function template interchangeable with the template function. – Rohan Bari Nov 27 '22 at 15:25
  • @RohanBari Yes, some authors use the term "template function" interchangeably with "function template". And some authors don't. I too don't think they are interchangeable at all. – Jason Nov 28 '22 at 01:55
  • @RohanBari From [FAQ](https://womble.decadent.org.uk/c++/template-faq.html#phrase-order): *"The term "function template" refers to a kind of template. The term "template function" is sometimes used to mean the same thing, and sometimes to mean a function instantiated from a function template. This ambiguity is best avoided by using "function template" for the former and something like "function template instance" or "instance of a function template" for the latter."* – Jason Nov 28 '22 at 01:56

1 Answers1

3

how does it work and is it legitimate code?

When we initialize or assign a function pointer from a function template, the compiler uses the type of the pointer to deduce the template argument(s).

For example, say we have a function pointer that points to a function returning an int and having 2 parameters with first parameter being int and second being double.

Then we can use that function pointer to point to an instantiation of template <typename T> T funcC(int a, double b){}.

template <typename T> T funcC(int a, double b)
{
    return a + b;
}
int (*ptrC)(int, double) = funcC; //ptrC point to an instantiation of funcC

In the above code T is deduced to be int(from the return type of the function type in the function pointer). Then ptrC points to a function instantiated(template<> int funcC<int>(int a, double b)) from the function template.

Example 2

On the other hand if you were to write:

double (*anythingDouble)(int,double) = funcC; //anythingDouble points to template<> double funcC<double>(int a, double b)

then T will be deduced to be double. Then anythingDouble points to a function instantiated(template<> double funcC<double>(int a, double b)) from the function template.

Jason
  • 36,170
  • 5
  • 26
  • 60