C++ templates

Jacek Wojdel wojdel at chrysler.kbs.twi.tudelft.nl
Fri Nov 17 07:58:54 GMT 2000


-----BEGIN PGP SIGNED MESSAGE-----

On Fri, 17 Nov 2000, you wrote:
> I as far as I know, templates can't be split up into header and source
> files... the template class and implementation must exist in one unit (i.e.
> a header file).  This is required by the compiler.  I have never been able
> to successfully split up a template class into two units.

It is a bit confusing, but it _is_ possible to split declaration and
implementation of template classes with gcc. There are in fact two methods, one
of which is to use '#pragma interface' and '#pragma implementation' in
appropriate places (see gcc info page under 'Template instantiations'). However
if you're overwhelmed by the number of compiler flags that you have to add and
confused by pragmas, chose the following 3-file approach:

my_class.h
--------------
/* In this file you declare everything *
 * just as in any other header file    */
template<class T>
class my_class
{
public:
 my_class();
};

my_class.cpp
----------------
/* Here you define all methods as in any   *
 * other source file, but there is a catch *
 * at the end of the file...               */
#include "my_class.h"
template<class T>
my_class<T>::my_class()
{
  // do your stuff here...
}

/* This include is essential to make sure *
 * that the compiler not only gets the    *
 * idea of the templates but also         *
 * instantiates them.                     */
#include "my_class_instantiations.h"

my_class_instantiations.h
-------------------------
/* This file is just a list of any class   *
 * instantiations that you use in your     *
 * project. You have to update it manually *
 * each time you want to use your template *
 * with a different data type.             */
template class my_class<int>;
template class my_class<char>;
template class my_class<int*>;

Ok, I know you can just type the instantiations in the .cpp file instead of
including them from separate one, but it seems a bit cleaner to me in this way.
Regards,
	Jacek

--
+-------------------------------------+
|from: J.C.Wojdel                     |
|      J.C.Wojdel at cs.tudelft.nl       |
+-------------------------------------+


-----BEGIN PGP SIGNATURE-----
Version: PGPfreeware 5.0i for non-commercial use
MessageID: ErRq4ujVMMEp0uA+0lp3DexwA2KR80dW

iQCVAwUBOhTrCIWy7tU6a5rbAQHNgQP/TRVLNGkM7KpZH2p7tF9NR55JcwTzlUDR
1Dj1L3mx/pJVrJ8ac6GS0n/d4Ttj4pFfzqG6h/z6//MU+mYc35/Cz7B01I6ZBhZO
cm3Br9KwksgviUTgqexRWa6Mrx0DWNPIV0F9qfrlqI/9+rdwAOZlVaII56RwfUTj
+s4RJ9dC7/w=
=HT0J
-----END PGP SIGNATURE-----

-
to unsubscribe from this list send an email to kdevelop-request at kdevelop.org with the following body:
unsubscribe »your-email-address«



More information about the KDevelop mailing list