[2 days left] What’s wrong with your cloud strategy? Learn why multicloud solutions matter with Nimble Storage.Register Now

x
?
Solved

template class

Posted on 2000-04-22
2
Medium Priority
?
265 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

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

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

This article will show you some of the more useful Standard Template Library (STL) algorithms through the use of working examples.  You will learn about how these algorithms fit into the STL architecture, how they work with STL containers, and why t…
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 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.
Suggested Courses

656 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