TITLE: Definitions and object files for templates PROBLEM: raymond@es.ele.tue.nl (Raymond Nijssen) Given: ---- ape.h -------------------------------------------------------- template class ape { public: ape(); }; ---- ape.cc ------------------------------------------------------- #include "ape.h" template ape::ape () {}; ---- main.cc ------------------------------------------------------ #include "ape.h" #ifdef unnecessary #include "ape.cc" #endif int main() { ape chimp; } ------------------------------------------------------------------- Now, if the _definition_ of the constructor of the ape class isn't textually included in the file in which the template is instantiated, the linker will complain that there's no code for this constructor in ape.o. And, indeed: % nm ape.o Symbols from ape.o: Name Value Scope Type Subspace LS$START_001_000$ | 0|static|data |$CODE$ LS$START_002_000$ |1073741824|static|data |$DATA$ LS$START_001_001$ | 0|static|data |$LIT$ That's all ... no code was generated for the ctor! This, and other observations lead to the conclusion that templates are to be regarded as almost old-fashioned macros, in contrast to what the ARM made me believe (or should I say `hope'?). Accordingly, the only way to make this work is to #include all files containing _definitions_ (so just the _declarations_ is not enough, as the ARM implicitly suggests) in files in which those templates are used. If this is true, then I'm afraid the design of the concept of templates has some heavy repercussions for any programmer using it: you'll have to #include almost all *.h and *.cc files into most .cc files, which is little different from putting all your code into one big file, particularly wrt. compiling it. Bjarne, are you listening? Or can somebody else explain: 1) Should the programmer regard templates as old-fashioned macros? 2) How are templates dealt with by the compiler? 3) How does this affect coding style? Or... is gcc 2.2.2 flaky wrt. templates? RESPONSE: p j l @sparc22.cs.uiuc.edu (Paul Lucas), 28 Sep 1992 AT&T Bell Laboratories It's implementation-dependent. For example, in AT&T's C++, you don't include the .c files, you just tell it where they are via the usual -I directive...it does the rest. The only difference is that you put the .c files into the include directory right along with the .h files...hardly a change in coding style or anything else. Macros are not type-safe; macros have no knowledge of the underlying language, etc. I'd suggest a more inquisitive tone, rather than accusatory in future. RESPONSE: ark@alice.att.com (Andrew Koenig) This is an artifact of the implementation. For example, under cfront 3.0, I can do the following: -----------------------ape.h----------------------- template class ape { public: ape(); }; -----------------------ape.c----------------------- template ape::ape () {} -----------------------main.c----------------------- #include "ape.h" main() { ape chimp; return 0; } and I can say CC main.c and all will compile and link automatically.