Feb 17

Why C++ is a Dying Language

Category: C++, Programming

From my blog you may notice I am an inveterate C++ coder. It was the first language I taught myself after pascal and my preferred language for compact, fast code.The downfall of C++ may not be news to many, but often it is written by opponents of the language rather than those who love it and all of its quirks. So from a fan, C++, the prognosis is poor. I could write a book on this, but I tried to distill it down to article size.

Principle of Least Surprise

The first reason is that C++ violates the Principle of Least Surprise regularly and with malice of forethought. What do I mean by this? This compiles:

class ThreadPool{
public:
   ThreadPool(int thread_count);
   ...
};
int compute_bleah(int i){
  ...
}
int compute_blah(ThreadPool pool){
  ...
}
int main(){
   //oops...
   compute_blah(5);
};

Why does this compile? Because unless you put the word explicit before the constructor taking an int as an argument C++ automatically converts any int to a ThreadPool whenever given the choice.

When you bring up these issues, the C++ defender will typically blame the user for being ignorant. This is stupid. Sane languages should do the most obvious and least damaging thing by default and require explicit permission to do anything else. In this case the language does the most bizarre and stupid thing unless you explicitly tell it not to.

Now this may be a contrived example, but it is not an isolated one. If you are a seasoned C++ programmer you probably still run into something like this once every couple of years. In fact, C++ is so full of similar examples that people have created whole careers just enumerating them.

Efficiency by default

This is really more of problem #1, but it deserves its own section. Although C++ often does non-obvious things because it hates you, occasionally it does stupid and non-obvious things because it is trying to save you memory and/or processing power. A classic example of this is:

class A{
public:
  ~A();
  void doSomething();
};
class B:public A{
  ~B();
  void doSomething();
};
void clean_up(A *a){
  a->doSomething();
  delete a;
}
void main(){
  A *b_as_a = new B();
  clean_up(b_as_a);
}

This code will call A’s version of the doSomething method and the fail to call B’s destructor. Why? because virtual functions have a (very) slight cost in memory and performance so C++ does the most efficient thing by default rather than the least stupid. If you want things to work correctly, you have to explicitly tell the compiler to make the destructor and method virtual.

Now, don’t get me wrong. I know why this is. When C++ came out everyone in the C community was convinced that:

  1. Object oriented code could not be efficient
  2. noone should ever have or need more than 256K of RAM

As it turns out the C weenies were not convinced on #1 anyway and we all know about #2. Unfortunately, this causes a long-term problem for C++: Those who want efficiency have stuck with C regardless (embedded world, GNU UNIX, BSD, etc, etc) and those who don’t need efficiency but want and OO language (more and more every year) just use a modern language without all the cruft.

There is a niche that C++ can still play in the object oriented, resource constrained environment, but it isn’t clear that C++ is or will be the only player there anyway.For instance, D has most if not all of the benefits that C++ does (apart from being mature I suppose) but has done away with a lot of the stupidity.

Even if D doesn’t become a stable alternative,some other language that isn’t beholden to a pile of historical cruft will.

Not Created by a Despotic Organization

Funnily enough a lot of people think this is a good thing and make snide asides presumably aimed at Java and C#. C++ was designed and standardized by committee rather than an evil empire and I don’t really see that the process has helped things. C++ has a lot of useless features. I would propose the best example of this being exception specifications (see C++ exceptions article).

More damning however is that there were, and to some lesser extent are, a million compilers for the language not a single one of which actually implements the standard. This is a huge problem for C++ because it basically means you can’t use almost any features and retain portability.

If you don’t believe me take a look at mozilla.org’s Portability Guide. I can sum it up for you if you’d like: if you have to write C++ please use as little of it as possible and here’s another list of the million little reminders you have to continually pass the compiler not to screw you. This is not just an issue for Mozilla, I work on embedded software at NASA and they have basically the same document.

Despotic organizations do not have this problem. There is one official compiler and if your code compiles then it is correct. Done. No having to search through lists of standards and buying expensive third party tools only to have gcc break all you code in the next release because of some bit of template minutia.

No Standard Libraries

Again I understand the historical reasons for this, but this isn’t an article about how C++ would be great if it was still 1990. C++ has very little in the way of a standard library. What is does have you can’t use anyway because as per the previous point it wasn’t implemented consistently across compilers because they don’t support many of the features of the C++ language.

No threads. No sockets. No GUI. No big integer.

Now there are a large number of libraries provided by a number of different groups (boost and Microsoft being good examples). Unfortunately because C++ doesn’t provide a solid foundation for large libraries none of them share enough concepts to play well with each other. Each has its own smart pointers, template meta-programming libraries, etc.

To some extent java and C# have this same problem but it happens at a much higher level. Also given the less complicated nature of the language adapters are usually relatively easy to design.

Instruction

This is the final nail in the coffin. C++ is being taken out of the computer science curriculum. My own alma mater, University of Maryland, has removed it now and frankly the professors were pretty C++ illiterate anyway.

There has been a lot of hand wringing about what this means for our new programmers and in some cases I agree. The bigger picture though is that even those who have some reason to learn C++ will not be the sort of C++ experts you really need to deal with all these issues. These will be programmers who have been exposed to the relative sanity of Java and do not expect to have to fight a language every step of the way.

Libraries like boost are, to some extent ,usable by these non-experts, but they aren’t going to be maintainable by them. Using smart pointers is somewhat straight forward. Maintaining a cross platform compatible, template-based library us not. And given all the other issues with C++ (as well as the prevalence of garbage collection), why would you bother?

Of course the C++ minions will insinuate this is because they are a less expert variety of programmer, but expertise in programming today isn’t about reimplementing the wheel over and again in new and interesting ways. Today programming is about combining the vast expanse of reusable libraries and technologies into a maintainable system.

Conclusion

So will C++ still be around 5 years from now? Of course it will. There is a pretty good code base out there using it and there are still those that cling to it like limpets.

If you have something to prove, C++ provides a lot of pointless opportunities. On the other hand those who jumped ship from C to C++ are increasingly jumping from C++ to Java or C# (just look up some of the old C++ gurus and see what their blogs are about now) and those who still haven’t dumped C aren’t likely to suddenly get a passion for object oriented design.

There is also the matter of the new C++ standard coming up. It will add a lot of very cool features to the language and it will include a lot of what is now the boost library as part of the standard distribution.

Will this re-invigorate the language? Well it is now 2008 and only a few compilers have come close to implementing the last version of C++. It could very well take another 10 for any to get on top of the new one and when they do, it will still have all the historical syntactical baggage I love to hate.

So in conclusion, C++ will continue be a useful skill to have, particularly as the existing code base becomes expensive to maintain, but I’d be very careful of basing a career on it.

The Author

Michael Smit is a software engineer in Seattle, Washington who works for amazon

170 comments

170 Comments so far

  1. Christine March 31st, 2008 6:58 am

    I couldn’t agree with you more that languages should be written to prevent errors as much as possible. This is really exactly the same issue as GUI design – why allow (or in some cases encourage) unwanted behavior. C++ definitely allows you to shoot yourself in the foot. In addition – because c++ is really difficult to parse – writing a good editor that can flag some of these issues for you as you type is difficult.