C++ Apologetics

Posted by Daniel Lyons Mon, 11 Jun 2007 06:07:00 GMT

This is my third try to give an opinion about C++. Even having an opinion of this language is hard.

Firstly, C++ suffers from having evolved quite a bit during its life. A lot of the information out there about it is outdated. It’s not hard to find tutorials that have include files ending in .h and not adding a using namespace std; declaration. It’s also not hard to find example code for g++ that doesn’t compile.

Secondly, C++ is huge. Most people don’t use a fraction of its capabilities. A lot of its capabilities are rather new; there is a general fear of using new features for loss of compiler compatibility, which has been a problem for a long time with C++ because of the general size and complexity of the language. Even though it adds an additional pile of keywords onto C, the keywords it adds sometimes have different meanings in different contexts (virtual is the prime offender). In other places, you have to do some truly strange things to get C++ to do as you intend, pure virtual functions being defined with a tailing “= 0” in the class definition being the best example.

Partly because C++ is huge and partly because it has been around for so long, students are not exposed to all of it, or even necessarily the important parts of it. It takes a long time to learn, and with outdated or poor information at their disposal, people pick up bad habits. The popularity of the language only makes this a bigger problem.

Thirdly, and perhaps most importantly, C++ strives to maintain compatibility with C. This explains many of the faults of the language, especially the type/class dichotomy.

The language D is clearly trying to reimagine C++ without so much of the evolutionary baggage. That’s a noble goal, but by this late stage C++ compatibility is a big concern also. If you sacrifice that, you might as well sacrifice some of the other constraints and wind up with something more like OCaml.

Finally, C++ suffers from having some rather brain-dead defaults. The virtual declaration must be used extremely liberally to achieve what’s normally understood as OO—calling a function with a pointer to a parent class and having the instance’s class code get executed. The explicit keyword exists solely because the language is very friendly with implicit casting.

The more I learn about C++ , the more hilarious I find claims of Java’s superiority. If Gosling is such a genius, why did generics get added to Java so late in the game, mirroring evolutionarily their late addition to C++? I haven’t looked at it very closely, but I have trouble imagining that in Java you can do anything as cool as a templated average function which can average across any numeric-looking type in any data structure for which sequential access is possible.

I also remembered today one reason why I wanted to learn C++ back in the day: BeOS is completely written in it. Right now the only reason I can think of to use it would be to use ICE. For my purposes, C++ is basically defunct, unless I want to program in QT for some reason. Even that I’d have a hard time convincing myself to do, since I don’t care much to program for Windows or (frankly) Linux/BSD.

So again I’m having trouble trying to summarize my opinion. Could C++ have been a great language? Probably not, but it’s pretty good for what it’s trying to do. It’s not the fastest language on the planet, but it’s quite fast, often faster than its detractors claim. Much of the slow C++ out there is not using STL algorithms and data structures. Manual memory management is time consuming for programmers and the allocator thrashing can be more expensive on the processor than garbage collection, but this is largely a concession to C, and you can apparently add your own garbage collector in the form of a library anyhow. It has one of the most expressive inheritance mechanisms, perhaps second only to Eiffel’s, but comes at a cost of being harder to use (again the virtual keyword makes life interesting, along with the access control public/private/protected nightmare). It’s possible now, with the template system, to write terse, expressive code, but almost no one does, learning how is hard, and it produces code that’s quite hard to read. If you need speed, there are faster languages; if you need elegance, there are more elegant languages. Perhaps there is no alternative if compatibility is what you optimize for, but it’s hard for me to imagine wanting to start a new project in it. A truly mixed bag.

Tags , ,  | 1 comment

Comments

  1. Avatar Raoul Duke said 23 days later:

    I’m checking out Felix instead of D, because Felix supposedly tries to let you connect super easily to C++.

    http://felix.sourceforge.net/

    I’ve only gotten as far as downloading and discovering problems with the install under OS X on my old iBook :-)

(leave url/email »)

   Comment Markup Help Preview comment