Solved

Templates and multiple-file projects

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

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Visual c++ and text files 7 68
sorting efficency of sorting algorithm 30 113
How do i run a c++ file? 15 30
Where are the c++ header files to use in Eclipse? 4 10
Unlike C#, C++ doesn't have native support for sealing classes (so they cannot be sub-classed). At the cost of a virtual base class pointer it is possible to implement a pseudo sealing mechanism The trick is to virtually inherit from a base class…
Container Orchestration platforms empower organizations to scale their apps at an exceptional rate. This is the reason numerous innovation-driven companies are moving apps to an appropriated datacenter wide platform that empowers them to scale at a …
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 how to user default arguments when defining functions. This method of defining functions will be contrasted with the non-default-argument of defining functions.

803 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