?
Solved

template class

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

0
Comment
Question by:cyogesh
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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 200 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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Often, when implementing a feature, you won't know how certain events should be handled at the point where they occur and you'd rather defer to the user of your function or class. For example, a XML parser will extract a tag from the source code, wh…
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 additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.
Suggested Courses

765 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