C++ templates

Benjamin Ellis bmellis at uark.edu
Fri Nov 17 09:07:01 GMT 2000


On Friday 17 November 2000 01:58 am, you wrote:
> -----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"
>

hmm, why would you need to do this last part? that's wack.  if you want a 
my_class w/ type int... when you needed that object (say, in some function), 
you would say "my_class<int> nameOfObject;"  there is no need to do what you 
have here.  I have never seen anything like you've done here, and I use 
templates all the time.

> 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
>


you simple do this

----[my_class.h]------------------------------

template <class T> class MyClass;    // forward declaration

template <class T>
class MyClass
{
	public:
		MyClass();
};

---[EOF]------------------------------------

and for the class definition

----[my_class.cpp]-----------------------------

#include my_class.h

template <class T>
MyClass<T>::MyClass()
{
	// yeah, yeah, yeah....
}

----[EOF]---------------------------------------


I swear that's all you haev to do! I do it all the time.. No crazy stuff, 
just the ANSI Standard way of doing it -- No "instatiations"... that's why 
they're classes.. you declare a new instance when you need one where you need 
one.

-Ben

-
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