Solved

template class

Posted on 2000-04-22
2
256 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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Need some help with listview limit text 20 98
C Language combined operators 28 106
Dialogbox API leak? 18 81
trigs fail! I thought I knew how to do trignometry 3 56
Errors will happen. It is a fact of life for the programmer. How and when errors are detected have a great impact on quality and cost of a product. It is better to detect errors at compile time, when possible and practical. Errors that make their wa…
IntroductionThis article is the second in a three part article series on the Visual Studio 2008 Debugger.  It provides tips in setting and using breakpoints. If not familiar with this debugger, you can find a basic introduction in the EE article loc…
The goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.

895 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

Need Help in Real-Time?

Connect with top rated Experts

17 Experts available now in Live!

Get 1:1 Help Now