home *** CD-ROM | disk | FTP | other *** search
- Path: sparky!uunet!think.com!yale.edu!qt.cs.utexas.edu!cs.utexas.edu!ut-emx!jamshid
- From: jamshid@ut-emx.uucp (Jamshid Afshar)
- Newsgroups: comp.lang.c++
- Subject: Re: where do templates go?
- Summary: what techniques do compiler writers have planned for templates?
- Message-ID: <83714@ut-emx.uucp>
- Date: 16 Nov 92 01:18:29 GMT
- References: <DECHC00.92Nov9082600@tohi.DMI.USherb.Ca> <1992Nov9.174401.415@nrao.edu>
- Reply-To: jamshid@emx.utexas.edu
- Organization: The University of Texas at Austin; Austin, Texas
- Lines: 53
-
- In article <1992Nov9.174401.415@nrao.edu> cflatter@nrao.edu writes:
- >In article 92Nov9082600@tohi.DMI.USherb.Ca, dechc00@tohi.DMI.USherb.Ca (CHRISTIAN DECHAMPLAIN) writes:
- >>
- >>I'm using g++. Suppose I'm using many compilation modules. Where do the
- >>templates go? [...]
- >
- >You have to put the template code in the header files. The section of the ARM
- >you are referring to is part ofthe commentary and not the language definition.
- >I expect that CYGNUS will come up with better template handling features for
- >g++ in the future.
-
- I recommend #include'ing the .cc/.cpp files inside the header files.
-
- // List.h
- #ifndef List_H
- #define List_H
-
- #include <...>
-
- template<class T> class List { ... };
-
- template<class T> inline void List<T>::append(...
-
- #ifndef DONT_INCLUDE_CC
- #include "List.cc" // non-inline member function definitions
- #endif
-
- #endif // List_H
-
- I've been doing this under BC++ 3.1 and if I understand the above it
- also works under g++. I believe cfront (sort-of) does the #include
- automaticly, so you may have to define DONT_INCLUDE_CC when porting to
- cfront.
-
- At least under BC++, all the template functions and member functions
- in List.cc are compiled into any module that uses List<T>. The linker
- does only put one version of those template functions, for each T,
- into the final executable. While this method is very conveniant, it
- does mean long compile times since the non-inline List code for a
- particular T will be compiled in multiple modules. Therefore, for
- larger projects I use the alternative method of forcing expansion of
- the non-inline functions in one .cc file using #pragmas (this is a
- major hassle because of various size limitations).
-
- I am curious what techniques compiler writers are considering (or
- using now?) for solving this problem. That is, how will the
- non-inline template functions be compiled only once for a particular
- instantiation, without user intervention? It would be good if this
- compilation were done on the first occurance of a particular List<T>
- instead of waiting until the link.
-
- Jamshid Afshar
- jamshid@emx.utexas.edu
-