home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1992 #19 / NN_1992_19.iso / spool / comp / lang / cplus / 13189 < prev    next >
Encoding:
Text File  |  1992-09-01  |  2.6 KB  |  78 lines

  1. Newsgroups: comp.lang.c++
  2. Path: sparky!uunet!kithrup!mrs
  3. From: mrs@kithrup.COM (Mike Stump)
  4. Subject: Re: Template functions and g++ - why are they not exported????
  5. Reply-To: mrs@cygnus.com
  6. Organization: Cygnus Support
  7. Date: Wed, 02 Sep 1992 09:38:25 GMT
  8. Message-ID: <1992Sep02.093825.7275@kithrup.COM>
  9. References: <1992Aug24.123831.20134@ugle.unit.no>
  10. Lines: 66
  11.  
  12. The problem is that the linker cannot yet do template instantiation.
  13.  
  14. AT&T's cfront will link the file, and depending upon what globals are still
  15. undefined, go back compiler some more code, and try linking again, this process
  16. can be repeated n times.
  17.  
  18. g++ for now requires that it see a use in each file, to produce an
  19. instantiationin each file.  It will not notice at link time it missed a few
  20. needed definitions.  This is considered a known bug, and one day, will probably
  21. be fixed.
  22.  
  23. But until then, you have to ``show'' the compiler a use of what it needs in
  24. each file.
  25.  
  26. In article <1992Aug24.123831.20134@ugle.unit.no> bjornmu@idt.unit.no (Bj|rn P. Munch) writes:
  27. >I have just started to do add some ++ to my C programming, and have
  28. >stumbled on a problem that baffles me.
  29. >
  30. >I took some of the "pseudo"-templates from libg++ to make myself a
  31. >hash table or two.  I thought it would be a good idea to clone these
  32. >into real templates, so I did.
  33. >
  34. >It didn't work.  After ours of struggling, I found the reason: the
  35. >member functions _are_ created when the're supposed to be (I can see
  36. >that by doing "nm" on the object file), but g++ makes them static,
  37. >i.e., they are not exported to the linker.  Aaaargh.   :-(
  38. >
  39. >This simple file will demonstrate it:
  40. >---
  41. >#include  <iostream.h>
  42. >
  43. >template<class T>
  44. >void foo (T t)
  45. >{
  46. >  cout << t << '\n';
  47. >}
  48. >
  49. >void bar(char *str)
  50. >{
  51. >  foo (str);
  52. >}
  53. >---
  54. >
  55. >bar() is here exported, while foo<char *>() is not.  The same happens
  56. >to my non-inlined member functions.
  57. >
  58. >What gives?  From what Lippmann says about this (2nd ed., p198), I
  59. >should be able to create the functions in one file, and use them from
  60. >other files.  Is this a bug in g++ (v2.2.2, Sun4), or do I have to
  61. >do something weird to make it work?
  62. >
  63. >  What I _can_ do, is defining a derived class and explicitly redeclare
  64. >  all the non-inlined functions:
  65. >
  66. >  class OptTable : public VHSet<Option *> {
  67. >  public:
  68. >    Pix  add (Option *o)  { return VHSet<Option *>::add (o); };
  69. >  //....
  70. >  };
  71. >
  72. >  , and compile with -fno-default-inline to make them "real" functions.
  73. >  This is inelegant and tedios, and will add an extra function call.
  74. >
  75. >Who can give me some insight?  If there is no way to make it work,
  76. >I'll try the above trick (or skip the templates), but I'd rather not
  77. >have to.
  78.