Solved

Templates and multiple-file projects

Posted on 2004-10-12
5
579 Views
Last Modified: 2012-06-27
How can I handle Templates in multiple-file projects? Please explain...

0
Comment
Question by:msjammu
5 Comments
 
LVL 86

Accepted Solution

by:
jkr earned 125 total points
ID: 12288635
You need to provide the template implementation in the header files and include them in each file. Even though the C++ standard would allow to separate the implementation from the declaration, there's no compiler out there that can handle that.
0
 
LVL 12

Expert Comment

by:stefan73
ID: 12289339
Hi jkr,
g++ had something close with its #pragma interface and #pragma implementation. I think this was abandoned in the 3.x versions.

The problem is always that multiple instances of template methods or functions are created and must be discarded by the linker - otherwise the final executable would be totally bloated.

Some compilers, such as SUN's CC, create temporary directories to counter the problem. As long as the directory where you compile your files is the same for all involved .cpp or .C files, there are no multiple instances created.

Cheers!

Stefan
0
 
LVL 6

Author Comment

by:msjammu
ID: 12295163
Stefan,
you mean with g++ I can have templates with my multi-files project, with no problems?
I use VC++ 6.0 SP6. Does not it seam to support it??

Many Thanks !
0
 
LVL 39

Assisted Solution

by:itsmeandnobodyelse
itsmeandnobodyelse earned 125 total points
ID: 12300303
jkr has it right. If you want to provide easy-to-handle template classes put all implementation to the header files (directly in the class definition or as inline functions) that should be stored to a shared project directory (you may add that direcory to Tools-Options-Directories-Include Files or to the 'Additional Project Include Directories' in Project Settings). Then, by simply including the class headers you may use these templates in multiple projects.

All other solutions are much less flexible or differ between Platforms/Compilers, e. g. you can move class implementation to cpp files, *but* need to have separate cpp sources where you make instances of your template classes for any type you are using. There you have to include the cpp files (!) of your template class implementation. Some Compilers also have #pragma interfaces that were able to create template instances (the template code for a specific type).

Regards, Alex
 
0
 
LVL 6

Author Comment

by:msjammu
ID: 12305317
Thank Your all experts for there help
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

What is C++ STL?: STL stands for Standard Template Library and is a part of standard C++ libraries. It contains many useful data structures (containers) and algorithms, which can spare you a lot of the time. Today we will look at the STL Vector. …
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.

820 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