No, it's not the same as a regular function. With a regular function, you can declare
void foo(int);
void foo(double);
in a header, define the functions in some source file, such as foo.cc
, #include the header in any source file that has to use those functions, such as bar.cc
, and let the linker do the rest. The compiler will compile bar.cc
and produce bar.o
, confident that you've defined the functions somewhere, and if you haven't then you'll get a link-time error.
But if you're using a template:
template <typename T>
void foo(T) ...
try to imagine how that would work. The source files foo.cc
and bar.cc
are independent and know nothing about each other, except that they agree on what's in the headers they both #include (that's the whole idea). So bar.cc
doesn't know how foo.cc
implements things, and foo.cc
doesn't know what bar.cc
will do with these functions. In this scenario, foo.cc
doesn't know what type bar.cc
will specify for T. So how can foo.cc
possible have definitions for every typename under the sun?
It can't, so this approach isn't allowed. You must have the whole template in the header, so that the compiler can gin up a definition for foo(int)
, or foo(string)
, or foo(myWeirdClass)
, or whatever bar.cc
calls for, and build it into bar.o
(or complain if the template makes no sense for that type).
The same goes for classes.
The rules are a little different for template specializations, but you should get a good grip on the basics before trying the advanced techniques.