Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > Re: Keeping the template implementation in .C

Reply
Thread Tools

Re: Keeping the template implementation in .C

 
 
Rene Eng
Guest
Posts: n/a
 
      07-27-2003
Hi there.


"qazmlp" <(E-Mail Removed)> schrieb im Newsbeitrag
news:(E-Mail Removed) m...
> It is advised to have the template implementation inside header files.
> But, Say, I keep the implementation in .C file. Are there any run time
> problems expected out of this or this is simply a link time problem?
>


I'd say it would not work at all!
It is necessary to compile the whole template code with the type of the
template parameter known, otherwise the compiler can not generate the
correct object code and can not check if the template parameter meets all
requirements (like operators etc.)
In other words: You can create an object of a template module and then
use this object code for different instantiations of the template.

One solution to this problem is to place the definition in the .h file and
the implementation in the .cpp file, but then you have to include the
..cpp file from the .h file, exactly the other way round than it is normally
done.
May be confusing if somebody else is looking at the code.


> Also, I would like to confirm whether having the template
> implementation in .h will increase the executable size for any reason
> compared to its size when the implementation is kept in a .C file.


Since the latter is not possible there is nothing to compare against.
However, the size of your executable will be larger since each template
instantiation generates a new class with all the code of the template.


hth
René



=======================================
C++ sources, cross-platform (UNIX and WinTel) and
covering several topics: http://gemini.futurezone.com



 
Reply With Quote
 
 
 
 
Peter van Merkerk
Guest
Posts: n/a
 
      07-27-2003
> > It is advised to have the template implementation inside header files.
> > But, Say, I keep the implementation in .C file. Are there any run time
> > problems expected out of this or this is simply a link time problem?

>
> I'd say it would not work at all!
> It is necessary to compile the whole template code with the type of the
> template parameter known, otherwise the compiler can not generate the
> correct object code and can not check if the template parameter meets all
> requirements (like operators etc.)


That is not entirely true, if the compiler supports the export keyword,
template definitions can be put in a .cpp file. Unfortunately very few
compilers currently do support this keyword. AFAIK only compilers based on
the EDG front end support this keyword.

> In other words: You can create an object of a template module and then
> use this object code for different instantiations of the template.
>
> One solution to this problem is to place the definition in the .h file and
> the implementation in the .cpp file, but then you have to include the
> .cpp file from the .h file, exactly the other way round than it is

normally
> done.
> May be confusing if somebody else is looking at the code.
>
> > Also, I would like to confirm whether having the template
> > implementation in .h will increase the executable size for any reason
> > compared to its size when the implementation is kept in a .C file.

>
> Since the latter is not possible there is nothing to compare against.


Well it is possible, but it is questionable if it will make a big
difference, since basically every time a template is instantiated code
specific for that instantiation must be generated. The only potential saving
I can see is that it might be easier to share code that is independant of
template parameters.

> However, the size of your executable will be larger since each template
> instantiation generates a new class with all the code of the template.


There are ways to avoid, or at least reduce code bloat caused by template
classes.

--
Peter van Merkerk
peter.van.merkerk(at)dse.nl


 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
hiding non template dependent implementation in a template class er C++ 1 10-16-2007 01:15 AM
template template arguments: expected a class template, got `Component<T1, T2, T3> gary.bernstein@gmail.com C++ 1 06-08-2007 07:10 AM
Re: Keeping the template implementation in .C Mike Spencer C++ 0 07-29-2003 12:32 AM
Re: A Newbie Question about template template template tom_usenet C++ 0 07-24-2003 12:06 PM
Re: A Newbie Question about template template template Chris Theis C++ 2 07-24-2003 09:42 AM



Advertisments