STL & Generic Programming: That's All Folks

Thomas Becker

Recommendations and Good Advice from the Codin' Cowboy as he rides off into the sunset.

Errata

Not even in my farewell column am I spared the embarrassment of having to admit to an error. In Listing 3 of my April column, I wanted to show how to write a loop-unrolled version of the Russian peasant algorithm. The code there is incorrect. Listing 1 of this column shows a correct version.

Looking Back

When Marc Briand, then editor-in-chief of C/C++ Users Journal, first asked me if I wanted to write a column on the subject of the C++ Standard Template Library and generic programming, I was quite excited. I felt that I had learned a thing or two on these subjects that was worth writing up and sharing with other software engineers, and so I accepted his offer. We then worked out a general plan for the column. For the first year or two, I was to write about the STL. My column should not be a substitute for a beginners book; instead, I was going to write about the do's and don'ts and gotchas and advanced issues that one doesn't usually find in a beginner's book or in the online documentation. After that, I was to move on to more general issues in generic programming such as template metaprogramming, policy-driven design, and the like. That was almost exactly three years ago, and I have now completed the tour that we laid out back then.

What's Next?

Needless to say, you're never really done with a subject like mine. Things move on while you're trying to catch up with them. The most important development in generic programming over the last three years is undoubtedly the explosive growth of the Boost library. When I started this column, Boost consisted of Nicolai Josuttis' functional composition adapters for the STL. Three years later, it has grown into a cornucopia of generic programming tools that will quite literally give the productivity in your shop an enormous boost. At first I thought that I should write about the stuff that's in the Boost library in this column. But the Boost tools come with excellent documentation, and so really all that needs to be said is this: go get the Boost library, make yourself familiar with what's in there, and use it. Here at Zephyr Associates, we have a semi-regular brown bag lunch going on where we discuss current developments in software engineering, and we use that to keep each other current and informed on the Boost stuff [1].

Also, quite a few great books on the STL and on generic programming have come out over the last three years. As a result, the C++ programmer's list of must-read books (also known here at Zephyr Associates as the list of books which if you haven't read them we won't hire you) has grown. The classics are still there: you're not going anywhere if you haven't read Scott Meyers' first two books [2], the design patterns book [3], and Herb Sutter's two books [4]. In addition to that, you must now learn about the STL from an introductory book. There are several choices, and you should shop around, but I think it's fair to say that Nicolai Josuttis' STL book [5] is emerging as the standard introduction to the subject. If you really want to use the STL efficiently (and using it inefficiently is often worse than not using it at all), you can't stop there. You also need to read Scott Meyers' third book, Effective STL [6].

By the time you've gotten that far, you've been looking at a lot of template error messages [7], and you've been exposed to advanced generic programming concepts such as traits. Now, at the latest, is the time to pick up the ultimate book on C++ templates and their application in generic programming: David Vandevoorde's and Nicolai Josuttis' recent C++ Templates [8]. This is really two books in one. On the one hand, it is an incredibly authoritative, comprehensive, and well-written reference book that answers pretty much every question you could possibly have on C++ templates. On the other hand, it is organized in such a way that during a first pass, you can easily skip some of the gory details and save them for later. If you read just Part I and your picks from Parts III and IV, you get a great tutorial about templates and their applications that will go a long way in your daily work.

C++ Templates is also a prequel. It is the prequel to the ultimate book on creative generic programming, namely, Andrei Alexandrescu's Modern C++ Design [9]. You really need the kind of understanding of templates that David Vandevoorde and Nicolai Josuttis give you before you can attempt Andrei's book, but this guy is so ahead of the pack that his book came out two years before the first textbook that has the prerequisites. Andrei is the Jimi Hendrix of generic programming. He's got a six-string guitar, an amplifier and a few effects at his disposal, just like everybody else, but what he does with them just leaves you slack-jawed. Read his book and his ongoing column on the CUJ website [10].

Another great source of information and inspiration on C++ programming in general and generic programming in particular is the C++ seminar [11], which is now in its third year and pretty much set to become a regular event.

Looking Back

And that was pretty much all I had left to say: use Boost and read those great books. One little thing that is perhaps remarkable is this: Just when I was about done with the first part of my plan, talking about the do's and don'ts and gotchas of the STL, Scott Meyers' book Effective STL came out. And right about when I was done with the second part, talking about general applications of templates in generic programming, David Vandevoorde's and Nicolai Josuttis' C++ Templates came out. From hindsight, it almost looks as if the purpose of my column was to tide everyone over until the definitive books on the subject came out. Writing and publishing a good book always takes years. There is often a phase where people really need good information on a subject, but there aren't any good books out yet. Perhaps that is really what columns like this one are for, and if so, then perhaps I did something useful. And speaking of being useful, in looking back, I can only say what many teachers and writers before me have said to their students and readers: Thanks for being my audience, thanks for your feedback, and I can only hope that you have learned as much as I have.

Thanks

There are a few people whom I need to thank specifically. Marc Briand gave me the opportunity to do this in the first place. During his remaining tenure as editor-in-chief of CUJ, he has worked with me far beyond the call of duty to help me with the column. He has, on more than one occasion, saved me from making an even greater fool of myself than I normally do. When Joe Casad took over, he let me continue with my column for as long as I pleased. Chuck Allison has, on several occasions, found kind words for my column in his editorial, thus sending a few readers my way. And CUJ Managing Editor Amy Pettle and her recent successor Darrah Buren have done a great job turning the gibberish that I submitted to them into printable articles.

I never write about anything or teach anything unless I have not only good knowledge of the subject, but also solid practical experience with it. So how come I could claim solid practical experience with the STL in early 2000? That I owe to one guy whom I have to thank more than anybody else, and that's Aaron Moore. In 1997, when we started work on the Zephyr StyleAdvisor program, there were just the two of us. Had we had official titles, mine would have been "The Mad Mathematician," and his would have been "The Smarter Of The Two Of Us." Six years later, Aaron is CTO of Zephyr Associates, StyleAdvisor is standard software in the money management industry, and I'm still a mad mathematician (albeit one with a bank account). There are certainly many reasons for this remarkable success story, but one of the most important ones was Aaron's decision to make the STL and STL-style generic programming one of the cornerstones of our code design. If you were around in 1997, then you can appreciate what a visionary and bold decision that was. In 1997, it was not clear where things were going to go with the STL. In particular, it was not clear whether Microsoft's support for the STL would ever improve (or even continue, for that matter). In fact, that was not decided until 2002, when Microsoft finally made the wise decision to avail itself of Herb Sutter's advice and throw its weight behind C++ Standard conformance, including, of course, the STL [12].

There is a lesson here. To be a good engineer, you need, of course, knowledge and experience in your field. But there is one other thing that you need to be a truly excellent and successful engineer, and that's instinct. Sometimes, there just isn't enough information to make a decision based on reason alone. You have to just feel it in your guts which is the right way to go, and then have the courage to go with your instinct. The important thing is to recognize that kind of instinct and courage when you see it. If you don't find it in yourself, that's no big deal. Find a person who has it, then trust that person. Just don't pretend that you have it when you don't. There's too many people like that in the industry already.

Coda

And that's all folks. Time for this old codin' cowboy to saddle up and ride off into the sunset. Hope you had fun riding with me for a while, swapping those tall tales of generic programming and the STL over the campfire. I'll see you on the prairie.

References

[1] Like all good software companies, we're always looking for the best engineers. If you're the kind of person who gets excited about the latest addition to the Boost library, had an advance order in at Amazon for David Vandevoorde's and Nicolai Josuttis' book on templates, and writes killer code, then (and only then) send me an email at thomas@styleadvisor.com. Oh, and you have to want to live in Tahoe and go skiing during lunch break.

[2] Scott Meyers, Effective C++, Addison Wesley 1992, and More Effective C++, Addison Wesley 1996

[3] Erich Gamma, Richard Helm, Ralph Johnson, and John Vlissides, Design Patterns, Addison-Wesley 1994

[4] Herb Sutter, Exceptional C++, Addison Wesley 2000, and More Exceptional C++, Addison Wesley 2002

[5] Nicolai M. Josuttis, The C++ Standard Library: A Tutorial and Reference, Addison Wesley 1999

[6] Scott Meyers, Effective STL, Addison Wesley 2001

[7] You can make those mysterious STL template error messages much more understandable by using Leor Zolman's STL Filter tool <http://www.bdsoft.com/tools/stlfilt.html>

[8] David Vandevoorde and Nicolai M. Josuttis, C++ Templates: The Complete Guide, Addison Wesley 2002

[9] Andrei Alexandrescu, Modern C++ Design, Addison Wesley 2001

[10] <www.cuj.com/experts>

[11] <www.thecppseminar.com>

[12] I have been playing with Microsoft's VC++ 7.1 beta, and I am very impressed with their template support.

About the Author

Thomas Becker works as a senior software engineer for Zephyr Associates, Inc. in Zephyr Cove, Lake Tahoe. He can be reached at thomas@styleadvisor.com.