views:

265

answers:

2

I just downloaded the STL source code and I noticed all the definition for the STL template classes are included in the .h file. The actual source code for the function definition is in the .h file rather than .cpp/.c file. What is the reason for this?

http://www.sgi.com/tech/stl/download.html

+16  A: 

Because very few compilers implement linking of templates. It's hard.

Here's a brief but (I think) informative article about it: http://www.informit.com/guides/content.aspx?g=cplusplus&seqNum=53

I say "I think" because it's really not something I'm very familiar with other than that it's widely unimplemented. I initially said the standard didn't require it, but looking at the definition of "export" in C++03, I don't see any indication that it's optional. Maybe it's just a failed standard.

Steve Jessop
Thanks, just the other day I was actually wondering how c++ linked to templates!
Jeffrey Aylesworth
export is part of the C++98 standard, but as you stated most compilers don't support it (last I checked, the g++ team had no interest in implementing it).
Stephen Newell
Thank you very much
Judeo
The "export" keyword will most probably be deprecated in the next version of the C++ standard. See http://herbsutter.wordpress.com/2009/10/23/deprecating-export-considered-for-iso-c0x/
Phillip Ngan
Deprecated, heh. So if it's deprecated in C++0x, then it might even be altered in some way in "C++...um...maybe sometime after Duke Nukem Forever hits stores". By then, people might have figured out how to implement it ;-)
Steve Jessop
The 'comeau' compiler supports export. http://www.comeaucomputing.com/
Martin York
Isn't it the only one though ? The problem is that even if supported it is not useful for the STL anyway since its writers don't know with which types you want to instantiate the templates...
Matthieu M.
If it were supported, then it could be useful for template libraries. All compilers support linking classes and functions which are instantiations of templates. Linking an instance of a template doesn't use the template itself. The point of export is that you don't need to know the types in advance - you link against the template itself, and the linker instantiates it. To be precise, without export the template has to be defined in every compilation unit which uses it. With export, the template only has to be defined in one compilation unit.
Steve Jessop
And this is why it's so difficult to implement - the linker has to be able to instantiate the template, which pretty much means that it needs to have the source for the template, and a lot of context, and compile it at link time. Certainly when the STL was defined (before the C++ standard), no compilers implemented export. So SGI wrote it all in headers (a) because they wanted it to work on common compilers, and even more critically, (b) because they wanted there to exist a compiler which supported it...
Steve Jessop
+1  A: 

Think of templates as code generation. If you don't know beforehand what template will be used with, you can't compile. So you need to keep the implementation in the header.

This allows some inlining and that explains why sometimes using templated stuff (like std::sort) works faster than in plain C.

Tristram Gräbener
You are forgetting the (rather unfortunate) export keyword.
ChrisInEdmonton
I think people before arlready gave enough explanation and links about it, its non-implementation, and its probable future ;)
Tristram Gräbener