public inbox for gcc-help@gcc.gnu.org
 help / color / mirror / Atom feed
* RE: Templates
@ 2003-03-27 11:47 Ajay Bansal
  2003-03-27 14:13 ` Templates Matthias Oltmanns
  0 siblings, 1 reply; 7+ messages in thread
From: Ajay Bansal @ 2003-03-27 11:47 UTC (permalink / raw)
  To: Matthias Oltmanns, Matthieu Moy; +Cc: gcc-help, eccf

Still, if possible,

Can somebody post a working template example built with "--frepo
-fno-implicit-templates" 


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


^ permalink raw reply	[flat|nested] 7+ messages in thread

* RE: Templates
  2003-03-27 11:47 Templates Ajay Bansal
@ 2003-03-27 14:13 ` Matthias Oltmanns
  0 siblings, 0 replies; 7+ messages in thread
From: Matthias Oltmanns @ 2003-03-27 14:13 UTC (permalink / raw)
  To: Ajay Bansal; +Cc: gcc-help

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

^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: Templates
  2003-03-27  8:41 ` Templates Matthieu Moy
@ 2003-03-27  8:48   ` Matthias Oltmanns
  0 siblings, 0 replies; 7+ messages in thread
From: Matthias Oltmanns @ 2003-03-27  8:48 UTC (permalink / raw)
  To: Matthieu Moy; +Cc: gcc-help, eccf

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

^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: Templates
  2003-03-27  8:22 Templates Eduardo Cesar Cabrera Flores
@ 2003-03-27  8:41 ` Matthieu Moy
  2003-03-27  8:48   ` Templates Matthias Oltmanns
  0 siblings, 1 reply; 7+ messages in thread
From: Matthieu Moy @ 2003-03-27  8:41 UTC (permalink / raw)
  To: Eduardo Cesar Cabrera Flores; +Cc: gcc-help

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. 

-- 
Matthieu

^ permalink raw reply	[flat|nested] 7+ messages in thread

* Templates
@ 2003-03-27  8:22 Eduardo Cesar Cabrera Flores
  2003-03-27  8:41 ` Templates Matthieu Moy
  0 siblings, 1 reply; 7+ messages in thread
From: Eduardo Cesar Cabrera Flores @ 2003-03-27  8:22 UTC (permalink / raw)
  To: gcc-help



How to compile multiples files source code with templates classes and
functions in a makefile?

g++ -v
Reading specs from /local/gcc/lib/gcc-lib/i686-pc-linux-gnu/3.1/specs
Configured with: ./configure --prefix=/local/gcc
Thread model: single
gcc version 3.1


it works with g++ standalone  well, but when i try to complie with a
makefile or compile with options "-c" & "--frepo -fno-implicit-templates"
it fails!!!

in my ".h" file include my definitions of the funcion it means the ".C"
file

#include "my_definitions.C"


what is wrong?

Do u got some examples about compiling with a makefile with templates?

thanks in advance


cafe





^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: templates
  2003-02-11  7:59 templates Adrian Sandor
@ 2003-02-11 16:47 ` Nathan Sidwell
  0 siblings, 0 replies; 7+ messages in thread
From: Nathan Sidwell @ 2003-02-11 16:47 UTC (permalink / raw)
  To: Adrian Sandor; +Cc: gcc-help

Adrian Sandor wrote:
> let's say I have a simple template function:
> 
> template<class T>T sqr(T x){return x*x;}
> 
> then if I write "sqr(5)" then everything is ok
> but if I make some changes:
> 
> template<class T>struct dummy{typedef T type;};
> template<class T>T sqr(typename dummy<T>::type
> x){return x*x;}
> 
> then "sqr(5)" doesn't compile anymore; instead I have
> to write "sqr<int>(5)"
> what is the cause of this behaviour and how can I
> avoid it? (I need to use some type traits)
in handwaving terms, because template deduction doesn't recurse.
Consider if you had
	template<typename T> struct dummy{typedef T type;}
	template<> struct dummy<int> {typedef float type;}
that would be hard.
template <typename T> typename dummy<T>::type sqr (T);
would be deducible

please refer to a C++ news group/faq/list for further explanation

nathan

-- 
Nathan Sidwell    ::   http://www.codesourcery.com   ::     CodeSourcery LLC
          The voices in my head said this was stupid too
nathan@codesourcery.com : http://www.cs.bris.ac.uk/~nathan/ : nathan@acm.org


^ permalink raw reply	[flat|nested] 7+ messages in thread

* templates
@ 2003-02-11  7:59 Adrian Sandor
  2003-02-11 16:47 ` templates Nathan Sidwell
  0 siblings, 1 reply; 7+ messages in thread
From: Adrian Sandor @ 2003-02-11  7:59 UTC (permalink / raw)
  To: gcc-help


let's say I have a simple template function:

template<class T>T sqr(T x){return x*x;}

then if I write "sqr(5)" then everything is ok
but if I make some changes:

template<class T>struct dummy{typedef T type;};
template<class T>T sqr(typename dummy<T>::type
x){return x*x;}

then "sqr(5)" doesn't compile anymore; instead I have
to write "sqr<int>(5)"
what is the cause of this behaviour and how can I
avoid it? (I need to use some type traits)

thanks
Adrian

__________________________________________________
Do you Yahoo!?
Yahoo! Shopping - Send Flowers for Valentine's Day
http://shopping.yahoo.com

^ permalink raw reply	[flat|nested] 7+ messages in thread

end of thread, other threads:[~2003-03-27 11:47 UTC | newest]

Thread overview: 7+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2003-03-27 11:47 Templates Ajay Bansal
2003-03-27 14:13 ` Templates Matthias Oltmanns
  -- strict thread matches above, loose matches on Subject: below --
2003-03-27  8:22 Templates Eduardo Cesar Cabrera Flores
2003-03-27  8:41 ` Templates Matthieu Moy
2003-03-27  8:48   ` Templates Matthias Oltmanns
2003-02-11  7:59 templates Adrian Sandor
2003-02-11 16:47 ` templates Nathan Sidwell

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).