Lambda the Ultimate

inactiveTopic What's wrong with C++ templates?
started 5/27/2003; 9:47:39 AM - last post 6/2/2003; 10:17:06 AM
Kory Markevich - What's wrong with C++ templates?  blueArrow
5/27/2003; 9:47:39 AM (reads: 728, responses: 9)

A story taking the three most common uses of C++ templates and contrasting with features of other language that accomplish the same. He mentions ML's type inferencing, functors, and Lisp macros specifically.

Jacob Matthews - Re: What's wrong with C++ templates?  blueArrow
5/27/2003; 6:36:13 PM (reads: 722, responses: 0)
Hi -- I'm the author of that article, and I'd be interested to know what people here thought of it.

Darius Bacon - Re: What's wrong with C++ templates?  blueArrow
5/27/2003; 10:30:06 PM (reads: 690, responses: 0)
Nice article. Lisp macros go back to 1965, at least -- well before C.

Ehud Lamm - Re: What's wrong with C++ templates?  blueArrow
5/28/2003; 3:01:01 AM (reads: 678, responses: 0)
I've yet to see a creaful analysis of the difference between C++ templates and Ada generics. Thinking about the implications of the differences between the approaches taken by the two languages is very much worth your while.

Dan Shappir - Re: What's wrong with C++ templates?  blueArrow
5/28/2003; 8:43:53 AM (reads: 662, responses: 0)
With regard to the responses to the article, rather than the article itself, it seems that many object to C++ templates on the basis of resulting code bloat (see specifically the post from the former Taligent employee). The argument is that because the C++ compiler emits a distinct concrete implementation for each specific instantiation the result is code duplication.

Putting aside the obvious fact that one of the main reasons for using a high-level PL is writing a little source that translates into a lot of code, I would like to address this claim specifically.

I'm reminded of one of the initial arguments against C++, even before templates, that it generates code that is less efficient than C. When examined in detail, often these claims were made by newbie C++ programmers that wrote code in a way that resulted in lots of unnamed temporary variables (C++ can automatically and implicitly create unnamed variables as temporary value containers within an expression). These unnamed temporaries introduced overhead both in their creation and destruction.

It could usually be shown that many of these temporaries was superfluous and could be avoided through proper application of C++ coding techniques (yes, you do need to know C++ well in order to fully grasp the implications of what you are doing).

IMO the same is true for templates. Yes, you can write templates in such a way that code becomes bloated. But, using standard C++ features such as inheritance and partial template specialization, it's quit possible to write code in a way that doesn't bloat it. In fact, when combined with inlining and an optimizing compiler, the resulting code can often be more efficient and less bloated.

Oh, and yes, I did like the article. I think it would be useful, as Ehud has pointed out, to contrast C++ to the implementation of generics in a language that is closer to C++ than ML or Lisp, say Ada, Java or C#.

And Patrick is probably finding this whole discussion rather amusing.

Carl Witty - Re: What's wrong with C++ templates?  blueArrow
5/28/2003; 2:11:19 PM (reads: 626, responses: 1)
Let me post here the last paragraph of my comment at kuro5hin:

In conclusion: I like OCaml much better than C++. Basically, the only C++ feature I miss when writing OCaml code is templates. Most uses of templates could be replaced by ML generic polymorphism or ML functors, but the result would likely be significantly less efficient with many (perhaps all) ML compilers, and would in some cases be far more verbose. I would love to have a language in the ML family that supported C++-style templates (but this would be difficult, since the more interesting features of templates are difficult to combine with type inference).

Matt Hellige - Re: What's wrong with C++ templates?  blueArrow
5/28/2003; 2:49:41 PM (reads: 644, responses: 0)
I agree that there's not a complete congruence between what templates offer and what ML-variants currently offer. (The author of the original kuro5hin article realized this, too, which is why he also mentioned Scheme macros and so on.) One of the major differences is the performance penalty that ML programmers typically pay for the zealous use of functors that they'd like to indulge in. (I believe Paulson's "ML For the Working Programmer" discusses this in some depth.) The type of design that is really motivated by the language constructs is actually discouraged for performance reasons (I'm reminded again of OO languages that warn you not to make too many objects). However, even with all performance concerns addressed, functors still aren't macros. As I've mentioned elsewhere, there have been attempts to add macros to ML variants, and I believe this is still a very fruitful direction (check out MacroML).

Carl Witty - Re: What's wrong with C++ templates?  blueArrow
5/28/2003; 4:09:17 PM (reads: 624, responses: 0)
As I said in my kuro5hin comment:

As far as comparing C++ templates to Lisp/Scheme macros, I would say that they're not really comparable; there are many things which you can do with one but not the other. The main thing you can do with templates that you can't do with any other macro-style system I know of is compile-time dispatch based on the compile-time types of the chunks of program being manipulated. Lisp can't really do this, since Lisp doesn't really have a compile-time type system. (Maybe you could add a type inference system into your macro expansion, but the result would be clumsy and fragile.) It's difficult in ML (or any other language that uses type inference) as well, since you would have to mix macro expansion and type inference somehow.

I don't think MacroML (thanks for the pointer, by the way) lets you dispatch on types. Neither does Template Haskell, which I believe is essentially a Lisp-style macro system for Haskell.

Manuel Simoni - Re: What's wrong with C++ templates?  blueArrow
5/28/2003; 5:16:39 PM (reads: 607, responses: 0)
If I understand it right, using the upcoming G'Caml-OCaml extension you can dispatch on types.

GCaml currently works with an older version of the Ocaml toplevel, but will be updated. (see this message.)

Carl Witty - Re: What's wrong with C++ templates?  blueArrow
6/2/2003; 10:17:06 AM (reads: 564, responses: 0)
I skimmed Furuse's thesis (on the topic of GCaml) over the weekend. Very cool stuff! I'll look forward to playing with it when it's updated for the current OCaml.

I note that the implementation described in the thesis does run-time type dispatch. I would prefer an implementation which did compile-time specialization. (An experiment done by Mark Jones, in the context of Haskell type classes, showed that compile-time specialization both increased speed and reduced total code size. This would surely depend on programming style, etc.; but it's an interesting result.)