7

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?

1
  • 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
    Commented Nov 29, 2013 at 23:13

6 Answers 6

4

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.

3
  • 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
    Commented Nov 27, 2011 at 12:57
  • 1
    This answer is kinda wrong... There is a difference. Check my answer downward.
    – BugShotGG
    Commented Nov 29, 2013 at 16:49
  • @GeoPapas Thanks for the answer. I agree there is a difference this has been asked in my exam. Commented Jun 3, 2015 at 0:58
10

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 data) {...}

but actually:

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

You can read more here:

Template terms

Template Tutorial

3

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.

1

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.

1
  • While you're not writing a function, but a template which can be converted into a function by the compiler ;-)
    – johannes
    Commented Nov 27, 2011 at 13:28
-1

There is a little logical difference between template fuction and function template. Function template is the one that could be used to create a family of functions with different argument type. Syntax- template return type function_name (arguments
of type T) {...... } A function generated from a function template is called a template function.

1
  • This doesn't seem to add anything to what the existing answers have already said.
    – Ixrec
    Commented May 18, 2016 at 21:07
-2
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

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