Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Priority between normal function and Template function

In the following code, the main function uses normal function instead of Template function.

#include <iostream>

using namespace std;

template <class T>
void num(T t){cout<<"T : "<<t;}

void num(int a){cout<<"wT : "<<a;}


int main()
{
    num(5);
    return 0;
}

What is the possible reason behind this?

like image 382
IndieProgrammer Avatar asked Apr 24 '12 03:04

IndieProgrammer


People also ask

What is the difference between normal function and template function?

3. What is the difference between normal function and template function? Explanation: As a template feature allows you to write generic programs. therefore a template function works with any type of data whereas normal function works with the specific types mentioned while writing a program.

What is the main advantage of using template functions?

Some of the advantages of using templates are: Templates simplify the creation of documents. Templates can ease our workload and make us feel less stressed, and, at the same time, they increase efficiency. Templates increase the attention of the audience.

Are template functions slower?

Heavy use of templates can however lead to long compile times. Mostly right, but increased code size due to multiple instantiations of a template function can increase instruction cache misses, and slow down your program.

What is the difference between function and class template?

For normal code, you would use a class template when you want to create a class that is parameterised by a type, and a function template when you want to create a function that can operate on many different types.


1 Answers

Take a look at Herb Sutter's excellent article "Why not specialize function templates?"

To quote:

"Finally, let's focus on function templates only and consider the overloading rules to see which ones get called in different situations. The rules are pretty simple, at least at a high level, and can be expressed as a classic two-class system:

  1. Nontemplate functions are first-class citizens. A plain old nontemplate function that matches the parameter types as well as any function template will be selected over an otherwise-just-as-good function template.

  2. If there are no first-class citizens to choose from that are at least as good, then function base templates as the second-class citizens get consulted next. Which function base template gets selected depends on which matches best and is the "most specialized" (important note: this use of "specialized" oddly enough has nothing to do with template specializations; it's just an unfortunate colloquialism) according to a set of fairly arcane rules:

    • If it's clear that there's one "most specialized" function base template, that one gets used. If that base template happens to be specialized for the types being used, the specialization will get used, otherwise the base template instantiated with the correct types will be used.

    • Else if there's a tie for the "most specialized" function base template, the call is ambiguous because the compiler can't decide which is a better match. The programmer will have to do something to qualify the call and say which one is wanted.

    • Else if there's no function base template that can be made to match, the call is bad and the programmer will have to fix the code."

In your code example, as pointed out by David Z., the non-template function void num(int a) will be selected because it is matched in the first rule. Any additional function templates will only be considered if they would be a better match.

like image 159
TemplateRex Avatar answered Sep 19 '22 22:09

TemplateRex