Solved

Templates and multiple-file projects

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

0
Comment
Question by:msjammu
[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
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

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

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…
  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 …
The goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…
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.

707 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