Chris Handleys Deletions

This page contains things which ChrisHandley has deleted, because he doesn't feel comfortable permanently erasing stuff without giving other people a chance to undo it. Beware that this page will be emptied from time-to-time, so please undelete stuff you want while you have the chance! :-)


Templates are a smelly invention(?) of CeePlusPlus and would be avoided except that they are there to patch major flaws in CeePlusPlus's design. Discuss!

Actually, templates (generics) weren't an invention of CeePlusPlus at all. AdaLanguage had 'em first; many statically-typed FunctionalProgrammingLanguages have had GenericPolymorphism? for quite a while as well.

To get things rolling, here's my own take on why they smell:

-- ChrisHandley


I've done a lot of work with C++ templates, and although I have lots of complaints about them, I don't think a single one of the objections above holds water:

The main problems that I see with C++ templates are:

The second problem has been addressed to an amazing degree by various libraries, but they are torturous to write (and read), and the syntax for using them is awkward.

For the kind of work that I do these days, the degree of efficiency that can be achieved by C++ templates really isn't important. I use them more in cooperating systems of templates to create library interfaces that extend the language, or in smaller independent chunks to avoid code duplication. This sort of work would likely be far easier in a language like Lisp. But I see C# in our future -- which actually will probably be fine for the bulk of our application, but I think that I'll miss the expressive power of a more complex language.

-- DanMuller


Having read-up on Generics as implemented in Java (from http://java.sun.com/j2se/1.5/pdf/generics-tutorial.pdf ), I have to say that Generics are great, and do exactly what Templates should have done in CeePlusPlus. In particular, I found this very interesting:

 : "It [the use of generics in Java] is misleading, because the declaration of a generic is never actually expanded in this way. There aren't multiple copies of the code: not in source, not in binary, not on disk and not in memory. If you are a C++ programmer, you'll understand that this is very different than a C++ template."

What this means is that Java's Generics are simply static (compile-time) checks, rather than CeePlusPlus's way of duplicating whole classes.

In CeePlusPlus, as Templates perform static (compile-time) code generation that is not dissimilar from macro expansion (something else that Java cleverly avoids), they unfortunately get (mis?)used by lots of CeePlusPlus wizards to do very clever (and very cryptic) things. So if you like ultra cryptic things, then it is a Good Thing (but I don't, so it Smells).

In summary, Generics are great, Templates smell.

-- ChrisHandley

Not to be rude, but it appears that you have formed your opinion (regarding Java generics) on the basis of an article or two....

An interesting comparison can be found at BruceEckel's blog, http://mindview.net/WebLog/log-0050. Bruce (the author of ThinkingInCeePlusPlus, ThinkingInJava, and a recent convert to PythonLanguage) doesn't much care for the Java implementation.

Previous rebuttal removed, since I need to look at this much closer... (currently I am (again) thinking Java's way is better)--ChrisHandley

I don't understand this obsession with the copying that's supposedly going on in C++. It's an implementation detail; conceptually, that's exactly what happens (in both Java and C++), but whether it actually happens or not is an implementation and quality-of-implementation detail. In that sense, the quote above is simply incorrect; there's nothing inherent about C++ templates that requires copying anything at all. With modern C++ compilers, it's rare to hear anyone complaining about bloated sizes of the final program due to templates. -- DanMuller

Fair enough. I don't claim to be an expert on Templates, but from my understanding of them, it seems that CeePlusPlus can use Templates to do far more things than Java can do with Generics. If one really wants, one can on-purposely use Templates to generate lots of code (in a similar way to macros), and this is certainly very clever - but I hold the view that a feature should never be overly complex (obtuse), and this is my problem with Templates (aside from their probable size inefficiency & horridly complex compiler implementation). I suspect all CeePlusPlusWizards? will disagree with my AllFeaturesShouldBeSimple principle, so we might as well stop the discussion here :-( -- ChrisHandley

BTW, if something is difficult to implement in a compiler, it is often difficult for the user to fully comprehend too. I believe this holds very well for Templates.


Long ago (which is when I lasted work with them), templated C++ code would generate humongously long symbols. While this is an implementation problem, it did make life hard for us poor coders at debugging time. (If this is no longer a problem, please rejoice at the march of progress and refactor/delete this comment.)

Most modern development environments will happily demangle the names for you.

Now that most of the implementation issues are worked out (a process that took quite a few years from the addition of templates to the AnnotatedReferenceManual? to most compilers doing them right), they are actually one of the cooler features of CeePlusPlus.

EditText of this page (last edited December 15, 2005) or FindPage with title or text search