Template Question

Eric Lemings eric@lemings.com
Thu Mar 27 14:16:00 GMT 2003


Hi,

Using g++, can templates not be compiled separately from files that use them
and linked like normal object files?  Or do the template source files have
to be included like header files in the source files that use them?  For
example,

::::::::::::::
Test01.h
::::::::::::::

template <class T>
void foo (const T& t);

::::::::::::::
Test01.cpp
::::::::::::::

#include "Test01.h"

template <class T>
void foo (const T& t) {
  // do something with t
}

::::::::::::::
Main.cpp
::::::::::::::

#include "Test01.h"

int main (void) {
  foo<long> (42);
  return (0);
}

Can g++ compile and link these files just as if foo() were a plain ol'
function?  The GCC documentation on this topic appears to be obsolete.

Thanks,
Eric.



More information about the Gcc-help mailing list