🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Article View: gnu.g++.bug
Article #125

Re: templates in g++

#125
From: Jason Merrill
Date: Sat, 04 Apr 1992 00:33
55 lines
2270 bytes
skohn@cs.ucsd.edu (Scott Kohn) writes:

> - Is there a way of getting one globally linkable instantiation
>   of a template class?

>...
>share the same code.  However, the current g++ 2.0 implementation
>generates local code only.  That is, g++ generates print() code in
>foo.o and also generates print() code in bar.o -- even though the
>code is identical (both are for an int template).

>I'm writing a large template class and I would really like to avoid
>this type of unnecessary code duplication.  Any suggestions?

My first suggestion would be to avoid writing any large template classes in
g++ until the template functionality reaches beta; it is extremely crippled
right now.

For instance, this program:

template<class T> class Bunk {
    T data;
  public:
    Bunk<T>& returnme() { return *this; }
};

main()
{
    Bunk<int> intbunk;
}

produces the compile errors:

bunk.cpp:6: parse error before `>'
bunk.cpp: In method `int & Bunk<int>::returnme ()':
bunk.cpp:6: cannot convert to a reference type
bunk.cpp:6: warning: control reaches end of non-void function

under G++ 2.1.

Failing that, my suggestion would be to look into using the #pragma
interface and #pragma implementation directives.  They may or may not have
any bearing on templates; I don't know much about them.

My next suggestion would be to put the function definitions in a .cc file
together with the declarations of all the instantiations you're going to be
using, and link that in with your program.

Borland C++'s linker is smart enough to see that the template
instantiations are duplicated and merge duplicates, but most UNIX boxes
don't have linkers that know about C++...

--
Jason Merrill
jason@jarthur.claremont.edu
Technical Details
Message-ID: <1992Apr4.003357.14377@muddcs.claremont.edu>
Path: arc1-ca.pugleaf.net!sparky!uunet!think.com!zaphod.mps.ohio-state.edu!cis.ohio-state.edu!jarthur.claremont.edu!jason
References: <31302@sdcc12.ucsd.edu>

Show Full Headers
Complete Article Headers
Newsgroups: gnu.g++.bug
Path: arc1-ca.pugleaf.net!sparky!uunet!think.com!zaphod.mps.ohio-state.edu!cis.ohio-state.edu!jarthur.claremont.edu!jason
From: jason@jarthur.claremont.edu (Jason Merrill)
Subject: Re: templates in g++
Message-ID: <1992Apr4.003357.14377@muddcs.claremont.edu>
Keywords: really stupid question
Sender: gnulists@ai.mit.edu
Organization: Harvey Mudd College
References: <31302@sdcc12.ucsd.edu>
Distribution: gnu
Date: Sat, 4 Apr 1992 00:33:57 GMT
Approved: bug-g++@prep.ai.mit.edu
Lines: 56