This is a read-only archive!

Mozilla C++ portability guide

I'm up to Chapter 18 of Stroustrup's The C++ Programming Language. The templates chapter was painful and I had to read it twice. I think I got it the second time through. Stroustrup talks about how people ask him how long it takes to learn C++ and he says (paraphrased) "a year or two probably; be happy, it's not as long as it takes to learn a spoken language or a musical instrument". It's still frustrating. The syntax and whatnot are so easy to learn, but the idioms and common practices take forever to ingrain.

Today in my somewhat futile half-hearted attempt to learn autotools, I chanced upon the Mozilla C++ portability guide It includes such advice as:

  • Don't use templates.
  • Don't use exceptions.
  • Don't use namespaces.
  • Don't use the C++ standard library, not even iostream.
  • Don't put assignments in if statements.
  • Use macros.

This is interesting, since it's the exact opposite of what Stroustrup writes. And Stroustrup also says he wrote this book in such a way as to demonstrate "standard" portable code. I guess I don't doubt that the Mozilla guys know what they're talking about, but if that's the extent you have to bend in order to write "portable" code, I hope I don't ever have to.

July 18, 2007 @ 12:48 PM PDT
Cateogory: Programming

3 Comments

Syd
Quoth Syd on July 21, 2007 @ 7:29 PM PDT

I happened to work at Netscape at the time this document was produced. Many of the items are dated; I don't, for example, agree today that avoid RTTI is a reasonable guideline.

I'm currently preparing a book on C++ portability that will be released by Prentice Hall, based on my experiences at Netscape. Of the items mentioned by Mozilla in that document, when it comes down to it, the only ones I mention are 1) take care regarding the sign of char types 2) use of NSPR (using an abstraction library is definitely a good thing and 3) fix your compiler warnings. Beyond that, I think anything that Stroustrup claims is safe (especially if portability means working with Visual C++, and GCC on Mac and Linux -- which is the case for a great many people).

After reading Stroustrup, I'd move on to the Effective C++ book by Scott Meyers. Read that book back to front, and then do it again twice more. Use Stroustrup as a reference, but read (and practice) what Meyers has to say until it is second nature. You'll be a better C++ programmer for it.

Brian
Quoth Brian on July 22, 2007 @ 11:47 AM PDT

Awesome. Thanks for the advice and clarification.

Ranjith Thai Valappil
Quoth Ranjith Thai Valappil on September 13, 2007 @ 2:07 AM PDT

I will be using new C++ compilers, which will be supporting RTTI. Is there still any problem in using RTTI(dynamic_cast all those things)?