Solved

Templates and multiple-file projects

Posted on 2004-10-12
5
576 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

What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

  Included as part of the C++ Standard Template Library (STL) is a collection of generic containers. Each of these containers serves a different purpose and has different pros and cons. It is often difficult to decide which container to use and …
Introduction This article is a continuation of the C/C++ Visual Studio Express debugger series. Part 1 provided a quick start guide in using the debugger. Part 2 focused on additional topics in breakpoints. As your assignments become a little more …
The goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.

762 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

20 Experts available now in Live!

Get 1:1 Help Now