Solved

template class

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

Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

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…
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
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 learn how to user default arguments when defining functions. This method of defining functions will be contrasted with the non-default-argument of defining functions.

758 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

18 Experts available now in Live!

Get 1:1 Help Now