Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the difference between a template class and a class template?

Tags:

c++

What is the difference between a template class and a class template?

like image 428
coder Avatar asked May 18 '09 20:05

coder


People also ask

What is a template class?

Definition. As per the standard definition, a template class in C++ is a class that allows the programmer to operate with generic data types. This allows the class to be used on many different data types as per the requirements without the need of being re-written for each type.

What is a template class used for?

Similar to function templates, we can use class templates to create a single class to work with different data types. Class templates come in handy as they can make our code shorter and more manageable.

What is function template and class template?

A template allows us to create a family of classes or family of functions to handle different data types. Template classes and functions eliminate the code duplication of different data types and thus makes the development easier and faster. Multiple parameters can be used in both class and function template.

Which of the following describes a difference between template function and template class?

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.


2 Answers

This is a common point of confusion for many (including the Generic Programming page on Wikipedia, some C++ tutorials, and other answers on this page). As far as C++ is concerned, there is no such thing as a "template class," there is only a "class template." The way to read that phrase is "a template for a class," as opposed to a "function template," which is "a template for a function." Again: classes do not define templates, templates define classes (and functions). For example, this is a template, specifically a class template, but it is not a class:

template<typename T> class MyClassTemplate
{ 
    ...
};

The declaration MyClassTemplate<int> is a class, or pedantically, a class based on a template. There are no special properties of a class based on a template vs. a class not based on a template. The special properties are of the template itself.

The phrase "template class" means nothing, because the word "template" has no meaning as an adjective when applied to the noun "class" as far as C++ is concerned. It implies the existence of a class that is (or defines) a template, which is not a concept that exists in C++.

I understand the common confusion, as it is probably based on the fact that the words appear in the order "template class" in the actual language, which is a whole other story.

like image 132
Not Sure Avatar answered Oct 18 '22 20:10

Not Sure


Bjarne Stroustrup, the creator of C++, says in his book 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 function template interchangeable with template function.

like image 23
SHH Avatar answered Oct 18 '22 21:10

SHH