home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1992 #27 / NN_1992_27.iso / spool / comp / lang / cplus / 16335 < prev    next >
Encoding:
Internet Message Format  |  1992-11-15  |  2.5 KB

  1. Path: sparky!uunet!think.com!yale.edu!qt.cs.utexas.edu!cs.utexas.edu!ut-emx!jamshid
  2. From: jamshid@ut-emx.uucp (Jamshid Afshar)
  3. Newsgroups: comp.lang.c++
  4. Subject: Re: where do templates go?
  5. Summary: what techniques do compiler writers have planned for templates?
  6. Message-ID: <83714@ut-emx.uucp>
  7. Date: 16 Nov 92 01:18:29 GMT
  8. References: <DECHC00.92Nov9082600@tohi.DMI.USherb.Ca> <1992Nov9.174401.415@nrao.edu>
  9. Reply-To: jamshid@emx.utexas.edu
  10. Organization: The University of Texas at Austin; Austin, Texas
  11. Lines: 53
  12.  
  13. In article <1992Nov9.174401.415@nrao.edu> cflatter@nrao.edu writes:
  14. >In article 92Nov9082600@tohi.DMI.USherb.Ca, dechc00@tohi.DMI.USherb.Ca (CHRISTIAN DECHAMPLAIN) writes:
  15. >>
  16. >>I'm using g++.  Suppose I'm using many compilation modules.  Where do the
  17. >>templates go?  [...]
  18. >
  19. >You have to put the template code in the header files.  The section of the ARM
  20. >you are referring to is part ofthe commentary and not the language definition.
  21. >I expect that CYGNUS will come up with better template handling features for
  22. >g++ in the future.
  23.  
  24. I recommend #include'ing the .cc/.cpp files inside the header files.
  25.  
  26.     // List.h
  27.     #ifndef List_H
  28.     #define List_H
  29.  
  30.     #include <...>
  31.  
  32.     template<class T> class List { ... };
  33.  
  34.     template<class T> inline void List<T>::append(...
  35.  
  36.     #ifndef DONT_INCLUDE_CC
  37.     #include "List.cc"  // non-inline member function definitions
  38.     #endif
  39.  
  40.     #endif // List_H
  41.  
  42. I've been doing this under BC++ 3.1 and if I understand the above it
  43. also works under g++.  I believe cfront (sort-of) does the #include
  44. automaticly, so you may have to define DONT_INCLUDE_CC when porting to
  45. cfront.
  46.  
  47. At least under BC++, all the template functions and member functions
  48. in List.cc are compiled into any module that uses List<T>.  The linker
  49. does only put one version of those template functions, for each T,
  50. into the final executable.  While this method is very conveniant, it
  51. does mean long compile times since the non-inline List code for a
  52. particular T will be compiled in multiple modules.  Therefore, for
  53. larger projects I use the alternative method of forcing expansion of
  54. the non-inline functions in one .cc file using #pragmas (this is a
  55. major hassle because of various size limitations).
  56.  
  57. I am curious what techniques compiler writers are considering (or
  58. using now?) for solving this problem.  That is, how will the
  59. non-inline template functions be compiled only once for a particular
  60. instantiation, without user intervention?  It would be good if this
  61. compilation were done on the first occurance of a particular List<T>
  62. instead of waiting until the link.
  63.  
  64. Jamshid Afshar
  65. jamshid@emx.utexas.edu
  66.