0

I want to have a template function defined in one file and used in many files. Does this work the same way regular function prototypes work? So I can define it once and just include the prototype in other files? I have the same question for classes, must I include the full defintion of a template class in each header file, just as I would for a class? Would it cause and error if I defined a template function twice in separate files or would this just go unchecked.

One more question, what is the format for a template function prototype?

Michael Petrotta
  • 59,888
  • 27
  • 145
  • 179
rubixibuc
  • 7,111
  • 18
  • 59
  • 98
  • http://stackoverflow.com/questions/3368883/how-does-this-size-of-array-template-function-work Shows an example of a template prototype where the definition is not actually needed at the end of the day and so the linker does not complain. – QuentinUK Feb 28 '16 at 11:22

2 Answers2

4

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.

Beta
  • 96,650
  • 16
  • 149
  • 150
  • Just to make sure if -- I define them differently in two seperate files which definition does it use, let's say even if do this by mistake? Is each defintion local to the file that instantiates the template? Why coudln't I define a prototype such as, template void foo(T); This seems to work in the most recent version of c++, just wanted to make sure it won't cause me any problem in the future. – rubixibuc Apr 25 '11 at 05:17
  • @rubix - You are absolutely not allowed to have definitions that are not equal. Usually you solve this by putting it in a header file and include that everywhere you use the template. – Bo Persson Apr 25 '11 at 05:32
  • @rubixibuc: yes, each definition applies in the source files that can see it. There will be conflict only if a source sees both. The compiler might allow a prototype like that (I don't know why), but if you try to use the function without providing the definition where the calling source can see it, the compiler will complain (if it doesn't, please let me know). – Beta Apr 25 '11 at 05:34
  • @Beta I'm using code blocks ide, the prototype is in one file and the defintion another, I'm guessing it can see it. I had another quesiton that I posted below, what is the significance of typename, the book I am using uses template, what is the importance of using class or typename? – rubixibuc Apr 25 '11 at 05:42
  • @rubixibuc: Prototype in one file, definition in another, invocation in a third that can't see the second? And does it actually run, and work as intended? As for "class" vs "typename", I *think* that every class name is a typename, but not the other way (e.g. "myWeirdClass" is a class name and a type name, "int" is a typename but not a class name), but I'm not certain. – Beta Apr 25 '11 at 05:49
  • @Beta prototype in one file with invocation, defintion in one file with invocation, it is actually giving expected errors, now when I invocate it in the file without the defintion with a different typename. So as expected it cannot instantiate the function for that new typename without the defintion. But since when I use the same typename, it appearantly can use the same function instantiation. – rubixibuc Apr 25 '11 at 06:00
2

See this FAQ. Especially, items 12, 13 and 14 deals with separating the declaration and definition of template functions.

Vijay Mathew
  • 26,737
  • 4
  • 62
  • 93
  • I'm reading throught the faqs, another reference I using replaces the use of "typename" with "class" ex -- template, what is the significance of this? :-/ – rubixibuc Apr 25 '11 at 05:30
  • That is a forbidden file "Forbidden You do not have permission to access the requested file on this server." Please only link to files the general public can access. – QuentinUK Feb 28 '16 at 11:19