Hi all,
Just wanted to get some discussion/opinions going on templates in C++. I'm thinking about functions but the same stuff applies to classes. Until like an hour ago, I (and as far as I can tell, a bunch of code example sites on the internet) was under the impression that template functions must have declaration/definition in the same file. Moreover this file must be #include'd by any file who wishes to use the template. This is b/c C++ only generates code for templated functions upon instantiation. So if I have
template<typename T> void foo(){ ...code...};
then code is only generated once I call foo():
foo<double>();
Thus the compiler has to be able to see the definition and the callsite to generate code in order to know what the template is & what to fill in for typename.
Putting template definitions in "header" files seems to lead to 2 related problems:
1) every time you #include a file containing source code (e.g., template defn), the compiler has to recompile that code. Include it 100 times? Compile it 100 times. This is bad if you only ever use like 1 or 2 different typenames for your template b/c then you're compiling 100 identical things.
2) Compiling 100 identical things then results in 100 copies of the same object code. Suddenly a million code caches cried out in terror and were suddenly silenced.
It's also not necessary. Enter "explicit instantiation":
http://www.parashift.com/c++-faq-lite/templates.html#faq-35.13
http://publib.boulder.ibm.com/infoc...language/ref/clrc16explicit_instantiation.htm
It solves 1) & 2). But now your template isn't so general b/c explicit instantiation requires you to specify every possible typename usage of your template. If you template over multiple parameters, this could be a lot of things. As pointed out here,
http://yosefk.com/c++fqa/templates.html#fqa-35.13
this solution makes templates more like glorified macros.
So I dunno. Have you guys used/heard of explicit instantiation? Are there any other pros/cons? Do people avoid this or are they ignorant of it? The object code duplication seems not so bad when you aren't in performance-critical regions (but if you don't care about performance, might as well be using managed code??). But increased compile times are always at least annoying.
Lastly, I think basically all of this discussion is just a specialized case of the issue griped about here:
http://yosefk.com/c++fqa/defective.html#defect-3
As you can see this guy clearly dislikes C++, haha. But anyway I was wondering why is compilation in C++ broken down into translation units that are basically individual files & also basically independent of other translation units? (Not counting linking b/c the compiler never sees the source code nor object code of externally linked functions.) I'm guessing this is leftover from the old days when huge multifile projects were just not a thing.
What is hard about having the compiler be able to associate declarations with unique, global definitions? When compiler runs across some class X in this translation unit & it has no idea what X is, the compiler has to be able to see the definition of X so it can compile the code & do its thing. It may have done the exact same operations in hundreds of other source files but the compiler has no way of knowing about that!
yosefk also mentions that "modern" languages do have this "look up" ability. Anyone know which languages those are & how they've implemented it?
-Eric
Just wanted to get some discussion/opinions going on templates in C++. I'm thinking about functions but the same stuff applies to classes. Until like an hour ago, I (and as far as I can tell, a bunch of code example sites on the internet) was under the impression that template functions must have declaration/definition in the same file. Moreover this file must be #include'd by any file who wishes to use the template. This is b/c C++ only generates code for templated functions upon instantiation. So if I have
template<typename T> void foo(){ ...code...};
then code is only generated once I call foo():
foo<double>();
Thus the compiler has to be able to see the definition and the callsite to generate code in order to know what the template is & what to fill in for typename.
Putting template definitions in "header" files seems to lead to 2 related problems:
1) every time you #include a file containing source code (e.g., template defn), the compiler has to recompile that code. Include it 100 times? Compile it 100 times. This is bad if you only ever use like 1 or 2 different typenames for your template b/c then you're compiling 100 identical things.
2) Compiling 100 identical things then results in 100 copies of the same object code. Suddenly a million code caches cried out in terror and were suddenly silenced.
It's also not necessary. Enter "explicit instantiation":
http://www.parashift.com/c++-faq-lite/templates.html#faq-35.13
http://publib.boulder.ibm.com/infoc...language/ref/clrc16explicit_instantiation.htm
It solves 1) & 2). But now your template isn't so general b/c explicit instantiation requires you to specify every possible typename usage of your template. If you template over multiple parameters, this could be a lot of things. As pointed out here,
http://yosefk.com/c++fqa/templates.html#fqa-35.13
this solution makes templates more like glorified macros.
So I dunno. Have you guys used/heard of explicit instantiation? Are there any other pros/cons? Do people avoid this or are they ignorant of it? The object code duplication seems not so bad when you aren't in performance-critical regions (but if you don't care about performance, might as well be using managed code??). But increased compile times are always at least annoying.
Lastly, I think basically all of this discussion is just a specialized case of the issue griped about here:
http://yosefk.com/c++fqa/defective.html#defect-3
As you can see this guy clearly dislikes C++, haha. But anyway I was wondering why is compilation in C++ broken down into translation units that are basically individual files & also basically independent of other translation units? (Not counting linking b/c the compiler never sees the source code nor object code of externally linked functions.) I'm guessing this is leftover from the old days when huge multifile projects were just not a thing.
What is hard about having the compiler be able to associate declarations with unique, global definitions? When compiler runs across some class X in this translation unit & it has no idea what X is, the compiler has to be able to see the definition of X so it can compile the code & do its thing. It may have done the exact same operations in hundreds of other source files but the compiler has no way of knowing about that!
yosefk also mentions that "modern" languages do have this "look up" ability. Anyone know which languages those are & how they've implemented it?
-Eric