Templates
Matthias Oltmanns
Mathias.Oltmanns.Oltmanns@sysde.eads.net
Thu Mar 27 14:13:00 GMT 2003
Am Don, 2003-03-27 um 12.16 schrieb Ajay Bansal:
> Still, if possible,
>
> Can somebody post a working template example built with "--frepo
> -fno-implicit-templates"
>
Ok, here is a short silly example:
all files in one directory:
Counter.h ---------------------------
#ifndef _Counter_h
#define _Counter_h
template <typename Number>
class Counter
{
public:
Counter();
// increments and returns the counter
Number get();
private:
Number m_counter;
};
#include "Counter.cc"
#endif
Counter.cc ---------------------------------
template<typename Number>
Counter<Number>::Counter() : m_counter(0)
{
}
template<typename Number>
Number Counter<Number>::get()
{
return ++m_counter;
}
testCounter.cc ---------------------------------------------
#include <iostream>
#include "Counter.h"
int main(int, char**)
{
Counter<double> d;
Counter<long> l;
std::cout << "d: " << d.get() << std::endl;
std::cout << "d: " << d.get() << std::endl;
std::cout << "d: " << d.get() << std::endl;
std::cout << "l: " << l.get() << std::endl;
std::cout << "l: " << l.get() << std::endl;
std::cout << "l: " << l.get() << std::endl;
}
g++ -v
Reading specs from /usr/lib/gcc-lib/i386-redhat-linux/3.2/specs
Configured with: ../configure --prefix=/usr --mandir=/usr/share/man
--infodir=/usr/share/info --enable-shared --enable-threads=posix
--disable-checking --host=i386-redhat-linux --with-system-zlib
--enable-__cxa_atexit
Thread model: posix
gcc version 3.2 20020903 (Red Hat Linux 8.0 3.2-7)
g++ -o testCounter.o -c -frepo -fno-implicit-templates testCounter.cc
g++ -o testCounter testCounter.o
collect: recompiling testCounter.cc
collect: relinking
./testCounter
d: 1
d: 2
d: 3
l: 1
l: 2
l: 3
Did you really need the flag "-fno-implicit-templates" ? I've good
experiences without this flag.
cu
Matthias
>
> -----Original Message-----
> From: Matthias Oltmanns
> [mailto:Mathias.Oltmanns.Oltmanns@sysde.eads.net]
> Sent: Thursday, March 27, 2003 2:13 PM
> To: Matthieu Moy
> Cc: gcc-help@gcc.gnu.org; eccf@super.unam.mx
>
> Am Don, 2003-03-27 um 09.21 schrieb Matthieu Moy:
> > Eduardo Cesar Cabrera Flores <eccf@super.unam.mx> writes:
> >
> > > How to compile multiples files source code with templates classes
> > > and functions in a makefile?
> >
> > When you use templates, gcc needs to have all the template-related
>
> > code in the .h file.
> >
>
> Thats right but it's not the point Eduardo talked about. As i
> understand, he separates the template declaration from the definitions
> by placing the definition in a .C file *AND* includes the .C file from
> the .h file.
> For me this way it works fine as long as i place the header and template
> definitions files together in my include directories and do not mix them
> with my regular source files.
> Don't try to compile the template definition file. It should be enough
> to just use them.
>
> cu
> Matthias
>
> > --
> > Matthieu
> --
> Matthias Oltmanns
>
> Tel: 04421-1543-274
> mail: Mathias.Oltmanns.Oltmanns@sysde.eads.net
>
--
Matthias Oltmanns
Tel: 04421-1543-274
mail: Mathias.Oltmanns.Oltmanns@sysde.eads.net
More information about the Gcc-help
mailing list