home *** CD-ROM | disk | FTP | other *** search
- Newsgroups: comp.lang.c++
- Path: sparky!uunet!kithrup!mrs
- From: mrs@kithrup.COM (Mike Stump)
- Subject: Re: Template functions and g++ - why are they not exported????
- Reply-To: mrs@cygnus.com
- Organization: Cygnus Support
- Date: Wed, 02 Sep 1992 09:38:25 GMT
- Message-ID: <1992Sep02.093825.7275@kithrup.COM>
- References: <1992Aug24.123831.20134@ugle.unit.no>
- Lines: 66
-
- The problem is that the linker cannot yet do template instantiation.
-
- AT&T's cfront will link the file, and depending upon what globals are still
- undefined, go back compiler some more code, and try linking again, this process
- can be repeated n times.
-
- g++ for now requires that it see a use in each file, to produce an
- instantiationin each file. It will not notice at link time it missed a few
- needed definitions. This is considered a known bug, and one day, will probably
- be fixed.
-
- But until then, you have to ``show'' the compiler a use of what it needs in
- each file.
-
- In article <1992Aug24.123831.20134@ugle.unit.no> bjornmu@idt.unit.no (Bj|rn P. Munch) writes:
- >I have just started to do add some ++ to my C programming, and have
- >stumbled on a problem that baffles me.
- >
- >I took some of the "pseudo"-templates from libg++ to make myself a
- >hash table or two. I thought it would be a good idea to clone these
- >into real templates, so I did.
- >
- >It didn't work. After ours of struggling, I found the reason: the
- >member functions _are_ created when the're supposed to be (I can see
- >that by doing "nm" on the object file), but g++ makes them static,
- >i.e., they are not exported to the linker. Aaaargh. :-(
- >
- >This simple file will demonstrate it:
- >---
- >#include <iostream.h>
- >
- >template<class T>
- >void foo (T t)
- >{
- > cout << t << '\n';
- >}
- >
- >void bar(char *str)
- >{
- > foo (str);
- >}
- >---
- >
- >bar() is here exported, while foo<char *>() is not. The same happens
- >to my non-inlined member functions.
- >
- >What gives? From what Lippmann says about this (2nd ed., p198), I
- >should be able to create the functions in one file, and use them from
- >other files. Is this a bug in g++ (v2.2.2, Sun4), or do I have to
- >do something weird to make it work?
- >
- > What I _can_ do, is defining a derived class and explicitly redeclare
- > all the non-inlined functions:
- >
- > class OptTable : public VHSet<Option *> {
- > public:
- > Pix add (Option *o) { return VHSet<Option *>::add (o); };
- > //....
- > };
- >
- > , and compile with -fno-default-inline to make them "real" functions.
- > This is inelegant and tedios, and will add an extra function call.
- >
- >Who can give me some insight? If there is no way to make it work,
- >I'll try the above trick (or skip the templates), but I'd rather not
- >have to.
-