Take the 2-minute tour ×
Programmers Stack Exchange is a question and answer site for professional programmers interested in conceptual questions about software development. It's 100% free, no registration required.

I came across a question which asked the difference between Function Templates and Template Functions in C++ and also Template Classes and Class Templates.

I was under the impression that Function Templates and Template Functions were the same thing. Is there any difference between the two, if so what is it?

share|improve this question
    
The distinction arises in a book called C++ Templates - A Complete Guide (First print: 2002) It is my opinion that the question asker is referring to this book. –  rwong Nov 29 '13 at 23:13
add comment

5 Answers 5

up vote 4 down vote accepted

Is there any difference between the two, if so what is it?

No difference at all. If you google for it, you can see that somewhere it is defined as function templates and somewhere as template function.

I was under the impression that Function Templates and Template Functions were the same thing.

Your understanding was correct.

share|improve this answer
1  
I think "function template" and "class template" are the official terms, though. A "function template" is not really a function as "template function" would imply. –  UncleBens Nov 27 '11 at 12:57
    
This answer is kinda wrong... There is a difference. Check my answer downward. –  Geo Papas Nov 29 '13 at 16:49
add comment

template class is a term which should not be used, it used to designate what we are calling now a class template specialization since at least the 98 standard, but most people who are using it tend to use it as a synonym of class template (and that was true in the past as well, thus the introduction of another unambiguous term).


From the Annotated C++ Reference Manual (Ellis and Stroustrup, 1990):

A class template specifies how individual classes can be constructed.

A class generated from a class template is called a template class.

share|improve this answer
add comment

They are the same thing. The Standardese is "function template" and "class template", however many people (including myself) tend to refer to them as "template function" and "template class", for the simple and obvious reason that when you define one, first you write "template" and then you write "class", or a function. Hence, they are often referred to as both.

share|improve this answer
    
While you're not writing a function, but a template which can be converted into a function by the compiler ;-) –  johannes Nov 27 '11 at 13:28
add comment

There is a difference between function template and template function.

template<class Type>
void Foo(Type tData) {...}

if you wanted to pass int to function template Foo, but you also wanted the compiler to instantiate it as if it was passed a double you would call:

Foo<double> ( 12 );

Which instantiates the following template function:

void Foo(double);

With this special syntax Foo<>(), you are demanding compiler to instantiate Foo function for the type being explicitly passed, and asking the compiler not to deduce type by function argument.

A function template is body of a function that is bracketed around template keyword, which is not an actual function, and will not be fully compiled by compiler, and is not accountable by the linker. At least one call, for particular data-type(s) is needed to instantiate it, and be put into accountability of compiler and linker. Therefore, the instance of function template Foo is instantiated as Foo(int) or Foo(double).

A template function is simply an "instance of a function template", which is produced when you call it, or cause it to get instantiated for particular data type. The instance of function-template is actually a valid function.

An instance of a function template (aka template-function) is not a normal function, under the umbrella of name-decoration system of compiler and linker. That means, an instance of function-template:

template<class T> 
void Foo(T data) {...}

for template argument double, it is not:

void Foo<double>(double x) {...}

but actually:

void Foo<double>(double x) {...}

You can read more here:

Template terms

Template Tutorial

share|improve this answer
add comment
template <class T> T abs(T n) //function template
{ 
    return (n<0) ? -n : n; 
}

int main()
{
    int int1 = -10;
    cout << “\nabs(“ << int << “)=” << abs(int1);  //calling the abs function for type int 
}

When the compiler sees such a function call, it knows that the type to use is int because that’s the type of the argument int1. So it generates a specific version of the abs() function for type int, substituting int wherever it sees the name T in the function template.

This is called instantiating the function template, and each instantiated version of the function is called a template function. That is, a template function is a specific instance of a function template

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.