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 wOOdy-Soft on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Gcc templates instantiation

Status
Not open for further replies.

pifs

Programmer
Mar 13, 2003
6
BR
Hello,

I've have defined some templates in a .h file and implemented some of the declared functions in a separate .cpp file.

The problem is that the linker recognizes all those functions implemented in the .h
files (i.e. inline functions), but does not recognize the
ones in the .cpp files.

An example:

######################################################
"A.h" file:
template<T>
class A
{
void afunction(T xx){do_something(xx);} // This is ok
}
#######################################################

But if I have:
#####################################################
&quot;A.h&quot; file:
template<T>
class A
{
void afunction(T xx);
}

&quot;A.cpp&quot; file:
template<T> //This function is not recognized !!!
void A<T>::afunction(T xx)
{
do_something(xx);
}
#########################################################

Can anyone help me on that ?

Thanks!!!!
 
Yeah, according to Stroustrup 3ed you can use the &quot;export&quot; keyword in front of that .cpp definition but i have never seen it work in any of the compilers i've worked with. You can move that definition &quot;as is&quot; into your header file and it should work.


&quot;A.h&quot; file:
Code:
template<T>
class A
{
   void afunction(T xx);  
}

&quot;later in A.h&quot; file:
Code:
template<T>        
void A<T>::afunction(T xx)
{
    do_something(xx);
}

hope that helps
-pete
 
Here's gcc's page on how they do template instantiation.


I personally would love to have definitions in something other than a .h, as it seems wrong. Stroustrup in &quot;The Design and Evolution of C++&quot; concurs on p.376, by saying, &quot;Basically, template function definitions don't belong in header files.&quot;

The problem is that if the definitions are in header files, then all client code depends on the implementation. one small change to the implementation that does not affect the interface will cause a recompile. All that's really needed is the declaration (although then misuse won't be caught until link time as we don't have template argument constraints....)

But the easy implementation strategy is to require definition before use, as that way object files do not reinvoke the compiler at link time (generally a good thing).

I'd suggest you track down a compiler that suits your preferences. There are other free (for non commercial use) compilers. I don't know if any of them play nice.
 
Here's an article that shows a lot of your options for working with templates.



Note that EDG has actually implemented export. If you can get a compiler based on their technology, you'll be able to use it... but that article warns against doing so.

Export seems to cause a lot of problems, also mentioned in that article. I'd be surprised if they don't deprecate it or make some major changes to it in the next Standard.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top