Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Templates and multiple-file projects

Posted on 2004-10-12
5
Medium Priority
?
584 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 500 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 500 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

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

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

When writing generic code, using template meta-programming techniques, it is sometimes useful to know if a type is convertible to another type. A good example of when this might be is if you are writing diagnostic instrumentation for code to generat…
Introduction This article is the first in a series of articles about the C/C++ Visual Studio Express debugger.  It provides a quick start guide in using the debugger. Part 2 focuses on additional topics in breakpoints.  Lastly, Part 3 focuses on th…
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 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.

618 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