Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

template in precompiled header?

Status
Not open for further replies.

sandup

Technical User
Jun 7, 2003
24
RO
Say we have a template function that returns the maximum of two values.

template <class T> T& max (T param1, T param2) {...}

It's logical that it would compile into *two completely* separate functions if it's called once with integers, and once with, say, strings.

Then, if this template is included in a .H, the notion of &quot;precompiled header&quot; kind of goes down the drain, doesn't it?
 
this is the function implementation:
template <class T> T& max (T& param1, T& param2)
{
if(param1 >= param2)return param1;
return param2;
}

for any types of T, must be defined T::eek:perator >=(T&), so, if you have some class you want to use in template function max, just reloat this operator.

Ion Filipski
1c.bmp

ICQ: 95034075
AIM: IonFilipski
filipski@excite.com
 
Precompiled headers in not a part of the C++ language, it's implementation responsibility to make the work well (or do nothing, for example). It seems: don't worry...

How about the function returned reference to Type and accepted two by value args? What object will been returned from the function by reference? Args copies will be destroyed. You must define reference args if you want return reference (see min/max templates in STL).

 
I think a pre-compiled header would still work in the template case.

A pre-compiled header isn't really compiled. It's really the saved state of the compiler after having processed the header. Sorta of a &quot;head start&quot; for when you use that header (or set of headers) often.

When you make a pre-compiled header with a template function in it, the knowledge of that template is part of the state of the compiler at that time.

When you use that pre-compiled header to head start your compiler and it encounters a call to an instance of that template later in the code, it just instantiates it using the info it &quot;memorized&quot; from the pre-compile.

So yes, it still has to instantiate and compile each instance of the template, but you still get the head start. Nothing is different about templates than anything else, in that regard.


Keep in mind I've never used a pre-compiled header; I'm merely guessing what happens based on what I know about them. So don't be surprised if I'm completely wrong.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top