Is C++ still needed?

Christopher Baus is reasoning about why C++ is falling out of favor:

In domains where low level access is critical, developers are going for C. If developers can live with higher level abstractions, then they are skipping straight to Java or Python or Ruby or PHP, and by passing C++ altogether. C++ is getting squeezed out in the middle.

I think he’s right. If you’re doing large scale business applications, like I do on work, C++ definitely is not the language of choice. Some reasons:

  • C++ is slow. Amazing, but true. We evaluated both Java, C# and C++, and Java and C# were up to five times faster on usual use cases, like retrieving and instantiating objects from a database. This is because creating a new instance in memory takes much more time in C++ compared to other, even interpreted, languages - unless you don't write your own memory management. But as Christoper explains, C++ is missing some low level feature in this area, too ;-).
  • C++ takes more development time. Not only because you have to write your own memory management, there's much more to take care about in C++ than in other high level languages, mostly due to missing garbage collection. And a lot of time is consumed by compiling, which takes significantly longer than in any other language (talking about hours compared to minutes, actually).
  • C++ isn't open to other languages. While you can bridge from say Python to Java or from Java to C, C++ binaries cannot be accessed (unless they publish core C-functions). This is because there is no standard on how C++ should publish its library entry points (don't know if this is the right term), so each compiler does it slighly different.
  • C++ doesn't support module oriented development very well. For two reasons: First the interface definitions (the .h files) are cluttered with implementation stuff like private variables. If you work around this by holding your data in a separate structure, you pay with an additional memory allocation, which slows the application down. Second, C++ misses a concept like C# offers with "internal" (Java and VB offer same concepts): Code that is public within a module, but private from outside.

Nevertheless, we chose C++ as our language. Why? Because it was the only language that (using the QT framework) ran on Windows, Linux, and several (already outdated) *nix platforms, like for example HP-UX 20. Platforms where Java is available in version 1.0 only, if available at all. Platforms where Mono will never run. This however rather is a niche and more and more of these platforms are replaced by Linux nowadays.

Anyhow, there are cases were C++ comes in handy: All the high level languages and their libraries offer you about 90 percent of what you will need in everyday business programming. But if you want some advanced stuff like the most current juicy UI features or access to the systems network layer, you are left alone. There are reasons for this (for example the .NET framework must be compatible to Windows 98), of course and most languages will allow to use the system’s API, which is C.

However, using for example the Windows API from C# is a pain in the ass, since you must redeclare each and every structure and function. You must deal with pointers or pointers to pointers etc. All of which is native to C++ and saves you a bunch of time (and results in much cleaner code), as this comparison shows. In a recent project of mine I therefore happily mixed C++ and C#, the same as I did back when I was programming in Visual Basic 6.

You may note however, that I’m doing this because I can. C++.NET managed code can be used by C# without any problems (Dude, I really love it). For Visual Basic I created COM objects in C++ using ATL. But what if Java was my main language or Python? I’d be stuck (unless I’m doing CORBRA, which - well - isn’t really made for this kind of thing).

Conclusion? C++ sometimes is the best choice, sometimes not. If C++ is falling out of favor, then also because it didn’t care about the rest of the world - and still doesn’t care.

Published: November 24 2005