Solved

template class

Posted on 2000-04-22
2
260 Views
Last Modified: 2010-04-02
hi,
Why do we include the template class implementation file in header file?

0
Comment
Question by:cyogesh
2 Comments
 
LVL 1

Expert Comment

by:Zizzer
ID: 2740994
Because the implementation is necessary at compile-time in order to instantiate the template.  For example, in order to instantiate list<MyClass>, the implementation of the list template is needed by the compiler (e.g. in order to determine the size of the nodes in the linked list).

-- Zizzer
0
 
LVL 7

Accepted Solution

by:
KangaRoo earned 50 total points
ID: 2741427
Consider what happens in a project with two (or more) implementation files, the compiler first and seperately compiles these implementation files. Then the linker binds all function calls to the compiled functions and creates an executable.
Now with templates, nothing is done untill an actual specialization is instantiated by the code, somewhere:

template<typename T> void f(T t)
{cout << "template called: "<< t <<endl; }

If this template is not used it is not specialized (instantiated). A specializtion could occur next:

void g()
{
  int i = 3;
  f(i);
}

Now suppose you've neatly separated everything, a header with the declaration for the template:

template<typename T> void f(T t) ;

An implementation file with the definition

template<typename T> void f(T t)
{cout << "template called: "<< t <<endl; }

and an implementation that uses it

void g()
{
  int i = 3;
  f(i);
}

Now the compiler starts, first the template inplementation file, nothing happens since no specializations of the template are instantiated in that file.
Then the second file with g() is compiled, nothing special, code for g() is created and since there is a declaration for f() all is well for the compiler.
Then the linker will try to bind them. When inspecting g() it finds a function void f<int>(int) is referenced and it will look for that function. Which it can't find because the template was not specialized when the first file was compiled. Error, undefined reference... You know the deal.

When the implentation for the template is included in the header, the compiler will specialize it when compiling an implememtation file that uses it. This can result in duplicate definitions, but the linker will remove these duplicates. Thus solving the problem.

Note that the C++ standard actually wants the the template definitions in translation units (implementation files), just as non-templates but it makes the exception that 'compilers may require template definitions to be included in every translation unit that uses them'.

Does that answer your question?
0

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Getting IP address 8 89
C++ standard library based binary archive format 6 104
C++ Class Serialization, Encapsulation and Formatted Output 8 78
Header of docx file 17 115
Introduction This article is the first in a series of articles about the C/C++ Visual Studio Express debugger.  It provides a quick start guide in using the debugger. Part 2 focuses on additional topics in breakpoints.  Lastly, Part 3 focuses on th…
This article shows you how to optimize memory allocations in C++ using placement new. Applicable especially to usecases dealing with creation of large number of objects. A brief on problem: Lets take example problem for simplicity: - I have a G…
The viewer will learn how to use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.
The viewer will be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.

856 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question