# c++ question: templates and compilation

**URL:** <https://boards.straightdope.com/t/c-question-templates-and-compilation/128681>\
**Category:** Factual Questions\
**Created:** [September 20, 2002, 12:28am UTC](https://boards.straightdope.com/t/c-question-templates-and-compilation/128681 "2002-09-20T00:28:55Z")\
**Posts on this page:** 20\
**Page:** 1

<div class="post-metadata">

**Author:** ![ultrafilter](https://avatars.discourse-cdn.com/v4/letter/u/3d9bf3/32.png) [@ultrafilter](https://boards.straightdope.com/u/ultrafilter)\
**Post date:** [September 20, 2002, 12:28am UTC](https://boards.straightdope.com/t/c-question-templates-and-compilation/128681/1 "2002-09-20T00:28:55Z")

</div>

I’ve got a question about templates in C++. Why do you need to put template class implementations in the same file as the declarations? Why can’t you just compile them like any other class? I used to have a pretty good idea why, but for the life of me I can’t remember, and none of my references are any help.

---

<div class="post-metadata">

**Author:** ![c\_goat](https://avatars.discourse-cdn.com/v4/letter/c/f4b2a3/32.png) [@c\_goat](https://boards.straightdope.com/u/c_goat)\
**Post date:** [September 20, 2002, 1:28am UTC](https://boards.straightdope.com/t/c-question-templates-and-compilation/128681/2 "2002-09-20T01:28:46Z")

</div>

Basically, it’s because a template is just a pattern for creating an actual function. The compiler creates a new function for each type as it is needed. So the template itself doesn’t get compiled.

---

<div class="post-metadata">

**Author:** ![ultrafilter](https://avatars.discourse-cdn.com/v4/letter/u/3d9bf3/32.png) [@ultrafilter](https://boards.straightdope.com/u/ultrafilter)\
**Post date:** [September 20, 2002, 3:44am UTC](https://boards.straightdope.com/t/c-question-templates-and-compilation/128681/3 "2002-09-20T03:44:22Z")

</div>

I figured it had something to do with that. I need to look into the specific error messages before I can get a more specific question.

---

<div class="post-metadata">

**Author:** ![Urban\_Ranger](https://avatars.discourse-cdn.com/v4/letter/u/e9c0ed/32.png) [@Urban\_Ranger](https://boards.straightdope.com/u/Urban_Ranger)\
**Post date:** [September 20, 2002, 4:56am UTC](https://boards.straightdope.com/t/c-question-templates-and-compilation/128681/4 "2002-09-20T04:56:10Z")

</div>

Why use C++ when you can use Java?

---

<div class="post-metadata">

**Author:** ![ThisIsDrRemulak](https://avatars.discourse-cdn.com/v4/letter/t/50afbb/32.png) [@ThisIsDrRemulak](https://boards.straightdope.com/u/ThisIsDrRemulak)\
**Post date:** [September 20, 2002, 5:38am UTC](https://boards.straightdope.com/t/c-question-templates-and-compilation/128681/5 "2002-09-20T05:38:27Z")

</div>

> [@](#):
>
> \*Originally posted by Urban Ranger \*  
> \*\*Why use C++ when you can use Java? \*\*

HAHAHAHAHAHAHAHAHAHA

Why use a shovel when you have a rake?

---

<div class="post-metadata">

**Author:** ![ThisIsDrRemulak](https://avatars.discourse-cdn.com/v4/letter/t/50afbb/32.png) [@ThisIsDrRemulak](https://boards.straightdope.com/u/ThisIsDrRemulak)\
**Post date:** [September 20, 2002, 5:40am UTC](https://boards.straightdope.com/t/c-question-templates-and-compilation/128681/6 "2002-09-20T05:40:40Z")

</div>

> [@](#):
>
> \*Originally posted by Urban Ranger \*  
> \*\*Why use C++ when you can use Java? \*\*

---

<div class="post-metadata">

**Author:** ![ThisIsDrRemulak](https://avatars.discourse-cdn.com/v4/letter/t/50afbb/32.png) [@ThisIsDrRemulak](https://boards.straightdope.com/u/ThisIsDrRemulak)\
**Post date:** [September 20, 2002, 5:41am UTC](https://boards.straightdope.com/t/c-question-templates-and-compilation/128681/7 "2002-09-20T05:41:57Z")

</div>

Wow, goofy double-post on my first post, and it won’t let me edit or delete the post. Looks like i’m off to a good start!

---

<div class="post-metadata">

**Author:** ![ultrafilter](https://avatars.discourse-cdn.com/v4/letter/u/3d9bf3/32.png) [@ultrafilter](https://boards.straightdope.com/u/ultrafilter)\
**Post date:** [September 20, 2002, 8:28pm UTC](https://boards.straightdope.com/t/c-question-templates-and-compilation/128681/8 "2002-09-20T20:28:34Z")

</div>

> [@](#):
>
> \*Originally posted by Urban Ranger \*  
> \*\*Why use C++ when you can use Java? \*\*

Ah, fer chrissakes, can we stay out of Turing’s tar pit? I use C++ for some things, and will use Java where it’s appropriate. Neither language is so godawful or wonderful that there’s always a clear choice, and that’s that.

---

<div class="post-metadata">

**Author:** ![Newton\_meter](https://avatars.discourse-cdn.com/v4/letter/n/57b2e6/32.png) [@Newton\_meter](https://boards.straightdope.com/u/Newton_meter)\
**Post date:** [September 20, 2002, 10:00pm UTC](https://boards.straightdope.com/t/c-question-templates-and-compilation/128681/9 "2002-09-20T22:00:58Z")

</div>

> [@](#):
>
> \*Originally posted by ultrafilter \*  
> \*\*Ah, fer chrissakes, can we stay out of Turing’s tar pit? I use C++ for some things, and will use Java where it’s appropriate. Neither language is so godawful or wonderful that there’s always a clear choice, and that’s that. \*\*

Actually, it was really funny that you asked a question about one of the ways in which C++ has a clear an unambiguous advantage over Java (parametric polymorphism through generics), and someone suggested you use Java.

I would daresay that _I_ wouldn’t use Java if I were leery of the extremely common and also _extremely_ silly downcasting required to simulate parametric polymorphism using subtype polymorphism in Java. And I might not want the excessive performance hit required from walking metaobject information that is required by Java to verify downcasts at runtime.

You know, things like that.

---

<div class="post-metadata">

**Author:** ![Newton\_meter](https://avatars.discourse-cdn.com/v4/letter/n/57b2e6/32.png) [@Newton\_meter](https://boards.straightdope.com/u/Newton_meter)\
**Post date:** [September 20, 2002, 10:00pm UTC](https://boards.straightdope.com/t/c-question-templates-and-compilation/128681/10 "2002-09-20T22:00:58Z")

</div>

> [@](#):
>
> \*Originally posted by ultrafilter \*  
> \*\*Ah, fer chrissakes, can we stay out of Turing’s tar pit? I use C++ for some things, and will use Java where it’s appropriate. Neither language is so godawful or wonderful that there’s always a clear choice, and that’s that. \*\*

Actually, it was really funny that you asked a question about one of the ways in which C++ has a clear an unambiguous advantage over Java (parametric polymorphism through generics), and someone suggested you use Java.

I would daresay that _I_ wouldn’t use Java if I were leery of the extremely common and also _extremely_ silly downcasting required to simulate parametric polymorphism using subtype polymorphism in Java. And I might not want the excessive performance hit required by walking metaobject information that is required by Java to verify downcasts at runtime.

You know, things like that.

---

<div class="post-metadata">

**Author:** ![Newton\_meter](https://avatars.discourse-cdn.com/v4/letter/n/57b2e6/32.png) [@Newton\_meter](https://boards.straightdope.com/u/Newton_meter)\
**Post date:** [September 20, 2002, 10:00pm UTC](https://boards.straightdope.com/t/c-question-templates-and-compilation/128681/11 "2002-09-20T22:00:58Z")

</div>

> [@](#):
>
> \*Originally posted by ultrafilter \*  
> \*\*Ah, fer chrissakes, can we stay out of Turing’s tar pit? I use C++ for some things, and will use Java where it’s appropriate. Neither language is so godawful or wonderful that there’s always a clear choice, and that’s that. \*\*

Actually, it was really funny that you asked a question about one of the ways in which C++ has a clear an unambiguous advantage over Java (parametric polymorphism through generics), and someone suggested you use Java.

I would daresay that _I_ wouldn’t use Java because if I were leery of the extremely common and also _extremely_ silly downcasting required to simulate parametric polymorphism using subtype polymorphism in Java. And I might not want the excessive performance hit required by walking metaobject information that is required by Java to verify downcasts at runtime.

You know, things like that.

---

<div class="post-metadata">

**Author:** ![Newton\_meter](https://avatars.discourse-cdn.com/v4/letter/n/57b2e6/32.png) [@Newton\_meter](https://boards.straightdope.com/u/Newton_meter)\
**Post date:** [September 20, 2002, 10:12pm UTC](https://boards.straightdope.com/t/c-question-templates-and-compilation/128681/12 "2002-09-20T22:12:07Z")

</div>

Ultrafilter, my previous three posts were an attempt to illustrate the template mechanism. You see, template code is not _true_ parametric polymorphism. It’s not compiled to a single function that works for all argument types, it is more like a macro that is instantiated at each type it needs to be.

---

<div class="post-metadata">

**Author:** ![ultrafilter](https://avatars.discourse-cdn.com/v4/letter/u/3d9bf3/32.png) [@ultrafilter](https://boards.straightdope.com/u/ultrafilter)\
**Post date:** [September 20, 2002, 10:22pm UTC](https://boards.straightdope.com/t/c-question-templates-and-compilation/128681/13 "2002-09-20T22:22:45Z")

</div>

> [@](#):
>
> \*Originally posted by Newton meter \*  
> \*\*Ultrafilter, my previous three posts were an attempt to illustrate the template mechanism. You see, template code is not _true_ parametric polymorphism. It’s not compiled to a single function that works for all argument types, it is more like a macro that is instantiated at each type it needs to be. \*\*

Right, I understand that. That explains why you have to have the template implementation of a function in the same file as the calls made to that function. But why does compiling a file that contains only a template implementation throw an error? Is that necessary, or just the way they decided to do it?

---

<div class="post-metadata">

**Author:** ![Newton\_meter](https://avatars.discourse-cdn.com/v4/letter/n/57b2e6/32.png) [@Newton\_meter](https://boards.straightdope.com/u/Newton_meter)\
**Post date:** [September 20, 2002, 10:56pm UTC](https://boards.straightdope.com/t/c-question-templates-and-compilation/128681/14 "2002-09-20T22:56:44Z")

</div>

> [@](#):
>
> \*Originally posted by ultrafilter \*  
> \*\*Right, I understand that. That explains why you have to have the template implementation of a function in the same file as the calls made to that function. But why does compiling a file that contains only a template implementation throw an error? Is that necessary, or just the way they decided to do it? \*\*

I knew you understood it, I was just trying to make it look like the triple post was intentional.

I don’t have any problem compiling a file consisting of only:

```auto

template<class T>
T id(T x) { return x; }

```

using either g++ version 3.1 or Microsoft C/C++ version 12.00.8186. Could it be something else? A wierd interaction of the template and something in a header file?

---

<div class="post-metadata">

**Author:** ![ultrafilter](https://avatars.discourse-cdn.com/v4/letter/u/3d9bf3/32.png) [@ultrafilter](https://boards.straightdope.com/u/ultrafilter)\
**Post date:** [September 20, 2002, 11:00pm UTC](https://boards.straightdope.com/t/c-question-templates-and-compilation/128681/15 "2002-09-20T23:00:14Z")

</div>

Using MS Visual C++ 6.0, I had problems treating a template class, say a linked list, like a normal class. I would either have to put everything in the header file, or do the following:

TemplateList.h:

#ifndef TEMPLATE\_LIST\_H  
#define TEMPLATE\_LIST\_H

// class declaration

#include “TemplateList.cpp”

#endif

TemplateList.cpp:

#ifdef TEMPLATE\_LIST\_H  
#define TEMPLATE\_LIST\_CPP

// class implementation

#endif

---

<div class="post-metadata">

**Author:** ![ForbiddenFruitsalad](https://avatars.discourse-cdn.com/v4/letter/f/ccd318/32.png) [@ForbiddenFruitsalad](https://boards.straightdope.com/u/ForbiddenFruitsalad)\
**Post date:** [September 20, 2002, 11:06pm UTC](https://boards.straightdope.com/t/c-question-templates-and-compilation/128681/16 "2002-09-20T23:06:53Z")

</div>

> [@](#):
>
> \*Originally posted by ultrafilter \*  
> \*\*
> 
> Right, I understand that. That explains why you have to have the template implementation of a function in the same file as the calls made to that function. But why does compiling a file that contains only a template implementation throw an error? Is that necessary, or just the way they decided to do it? \*\*

You can compile template function definitions separately as long as they are fully specialized or only used in the same translation unit. Not fully specialized templates need to have their definitions visible when you’re calling them because the compiler will generate the code at that time.

Why can’t a non-fully specialized function (or member functions in such a class) be seperately compiled? Because the compiler doesn’t know what code to generate for calls in other modules; it can’t very well generate code for all possible combinations of template parameters…

There are ways around this limitation. The compiler could for example generate a “code template” for the function definition and rely on the linker (which knows exactly what function definitions are needed) to generate the code. But that would make the linker considerably more complex and slow down build times (there are probably other disadvantages I can’t remember right now). No compiler I know of supports this.

---

<div class="post-metadata">

**Author:** ![ForbiddenFruitsalad](https://avatars.discourse-cdn.com/v4/letter/f/ccd318/32.png) [@ForbiddenFruitsalad](https://boards.straightdope.com/u/ForbiddenFruitsalad)\
**Post date:** [September 20, 2002, 11:59pm UTC](https://boards.straightdope.com/t/c-question-templates-and-compilation/128681/17 "2002-09-20T23:59:34Z")

</div>

> [@](#):
>
> \*Originally posted by ultrafilter \*  
> **Using MS Visual C++ 6.0, I had problems treating a template class, say a linked list, like a normal class.**

A word of warning: the template support in VC++ 6.0 is so horribly broken that it’s only slightly less painful to work with than poking your eyes out using pieces of broken glass. You won’t notice it for extremely simple templates like your linked list example, but trying anything even remotely sophisticated will require you to work around lots of compiler bugs and limitations. The template implementation in Visual C++ 7.0 (aka .NET) is considerably less buggy but still lacks several important features.

As you may know, the C++ Standard describes a keyword **export** that allows you to place template declarations in header files and definitions in the main source file. Alas, there were NO compilers with support for export until recently. But even when (or if) mainstream compilers implement export, it won’t give you true separate compilation for templates; at best it will give you the illusion of separate compilation (ie the compiler and/or linker will transparently force recompilation whenever necessary).

---

<div class="post-metadata">

**Author:** ![Ms2001](https://sea3.discourse-cdn.com/straightdope/user_avatar/boards.straightdope.com/ms2001/32/18463_2.png) [@Ms2001](https://boards.straightdope.com/u/Ms2001)\
**Post date:** [September 21, 2002, 12:29am UTC](https://boards.straightdope.com/t/c-question-templates-and-compilation/128681/18 "2002-09-21T00:29:22Z")

</div>

Isn’t it possible to explicitly instantiate template functions, allowing the functions to be compiled separately? For example…

template\<class T\> T max(T left, T right) { return (left \> right) ? left : right; }  
int max(int, int);  
char max(char, char);

Or is that only for template classes?

---

<div class="post-metadata">

**Author:** ![Ms2001](https://sea3.discourse-cdn.com/straightdope/user_avatar/boards.straightdope.com/ms2001/32/18463_2.png) [@Ms2001](https://boards.straightdope.com/u/Ms2001)\
**Post date:** [September 21, 2002, 12:35am UTC](https://boards.straightdope.com/t/c-question-templates-and-compilation/128681/19 "2002-09-21T00:35:17Z")

</div>

Isn’t it possible to explicitly instantiate template functions, allowing the functions to be compiled separately? For example…

template\<class T\> T max(T left, T right) { return (left \> right) ? left : right; }  
int max(int, int);  
char max(char, char);

Or is that only for template classes?

---

<div class="post-metadata">

**Author:** ![ForbiddenFruitsalad](https://avatars.discourse-cdn.com/v4/letter/f/ccd318/32.png) [@ForbiddenFruitsalad](https://boards.straightdope.com/u/ForbiddenFruitsalad)\
**Post date:** [September 21, 2002, 12:51am UTC](https://boards.straightdope.com/t/c-question-templates-and-compilation/128681/20 "2002-09-21T00:51:38Z")

</div>

> [@](#):
>
> \*Originally posted by Mr2001 \*  
> **Isn’t it possible to explicitly instantiate template functions, allowing the functions to be compiled separately?**

Yes - what you are thinking of is what I referred to as “fully specialized template functions” above. If you declare a fully specialized template function (or member function), the definition can be separately compiled. The recommended way to declare such functions are as follows:

template \<class T\> T max(T left, T right) { return (left \> right) ? left : right; }  
template \<\> int max(int, int);  
template \<\> char max(char, char);

Note that the last two functions are fully specialized instances of the original max template rather than regular overloaded functions (the difference is subtle).
