alphalist Blog

Where C Plus Plus excels


Why do people choose to code in C++ where there are many other languages available? In a recent CTO podcast, Bjarne Stroustrup, the creator of C++, talked (among other things) about what makes C++ so useful in the context of its strengths and weaknesses.He also discussed the past, present and future of C++.

This is a written extract of some of the ideas he talked about in the podcast. 

I created C++ because I needed something really good at a low level but still allowed a good level of abstraction. I was inspired by how C did low-level and how Simula did object-oriented programming and abstractions. I got to learn each language from the masters. At Bell Labs, I had Dennis Richie and  Brian W. Kernighan just down the corridor, and while I was doing my masters at Aarhus University, Kristen Nygaard was a visiting professor. I took the object-oriented concepts (classes and class hierarchies) from Simula and put them into C thereby speeding them up so that you could actually afford to use the abstraction mechanisms. A lot of the languages at the time and even today that allow high degrees of abstraction don't also allow you to run efficiently on various forms of hardware. This is what makes C++ so useful for complex programming

Why C++ works well for complex systems.

There is a reason why they use C++ in space where you can’t send a repairman. If the web app doesn't work too well - tough. But if the brakes under your car don't work well -  that must not happen. 

I think people make a mistake when they think programming and coding is one thing. Making a web app is very different from building the infrastructure for something. There's a more arty aspect to the web, whereas the infrastructure stuff is engineering.  I think people doing these two things should by and large be educated differently and they should be using different tools. If the web app doesn't work too well - tough. But if the brakes under your car don't work well -  that must not happen.   We have to make sure - and we do. I mean C++ is running on L2 and on Mars where you can't send a repairman -  that's the kind of thing I think about. - Bjarne Stroustrup, Creator of C++ , speaking on the alphalist CTO podcast #61

You have to know what you are good at though. For example, I know what interests me the most is going down to the hardware and building abstractions that are useful.  I don't have the capacity to also think about how to  best serve people with a light computer science education,- sort of everybody writing code the way you do with JavaScript and Python. That's not my world. I don't claim to be good at it. On the other hand, I helped them build their stuff. They have an infrastructure too, and that's where I get interested. One of my students built one of the first efficient JavaScript engines. A lot of data science, AI and  machine learning etc. (which I hardly know about) is not done directly in C++ but in TensorFLow which  is a C++ program. 

Why is C++ one of the most energy-efficient languages? 

 I'm not saying that every program in C++  is efficient. That would be absurd because not everybody needs to optimise to that extent. It would be a waste of time to spend a lot of time really doing the optimal tuning of a piece of code which doesn’t need it.  [for example]  If you have plenty of resources and plenty of time to calculate the answer, you just write less tuned code. But if you need to tune things - you can. -  Bjarne Stroustrup, Creator of C++ , speaking on the alphalist CTO podcast #61

C++ excels in its ability to optimise for energy efficiency. For example, if you are running a large server farm - let's say tens of thousands of processors - then your electricity cost is going to hurt you if you are using an inefficient language.  But there are always trade offs. Do you want to spend your money on building more and better software or do you want to spend it on electricity? It depends what your needs are and what you're willing to pay for. Maybe you don't have programmers that can write efficient code, or maybe you have free electricity or maybe you do not need microsecond response times or Maybe the battery on your cell phone is big enough. But I'm interested [in optimising C++]  where the answer to both questions is no - we need to do better.

When is C++ more efficient than C?

C++ is very good at computing things at compile time. Anything that has to do unnecessary computations at run time is going to be slower. There are examples of C++ beating FORTRAN in dense matrix dense linear algebra which is its domain and also cases where C++ does better than C for system programming. The GCC C compiler is actually written in C++.

Is C++ losing popularity?

Not really. You hear a lot about new languages, but it actually appears that the C++ user community grows faster than several new languages put together. It's easy to grow by a high percentage if you have hardly any users. But growing from 5 million to 7 million is not a dramatically large percentage increase.  Also, people talk about new languages much more than they talk about older languages So it appears to me like noise  - we always overestimate the use of newer things.

Does C++ ever borrow ideas from other languages?

I think borrowing directly doesn't seem to work. Because any individual feature fits into a type system, fits into other features that work together. You want sort of a dense network of facilities that works in combination so you can't just take something. 

Once through, we did think of using Java as inspiration when we needed a formal memory model for C++ to standardise the concurrent mechanisms that we have been using for decades.  I thought ``Java has got a good one - Let's take it.”. That would be an example of learning and taking facilities from another language.But it turned out that experts from Intel and IBM came and said, “No, you can't do that. Because if you take the Java model, you will slow down our JVMs by a factor of 2.5 (or something like that).”    In other words, if C++ had the same relatively simple model as Java, all Java code would slow down significantly. And you just don't do such things. So it took 2-3 years to build a memory model for C++ that allowed the very highly-tuned and very highly-optimised [C++] code [implementing JVMs] to actually work as fast as the hardware allowed. This, of course, is seriously criticised for being too complex as it's not as clean and simple as the Java one. But it is exactly what allows the Java one to be clean. Also, of course it took a long time. People criticise the committee for being too slow, but we had to solve the problem - the real problem.

Why do people criticise C++ so much?

We know the problems of C++ -  so everybody can criticise that and complain. But we don't know the problems of RUST and Go if/when they reach a new domain.

  [The root cause of] a lot of the complexities and so a lot of the problems in C++ is in the breadth of its use. - Bjarne Stroustrup, Creator of C++ , speaking on the alphalist CTO podcast #61

 It gets into things where other languages haven't gone yet and when they get there, they will  get some of the problems we had. For example, we worked very hard to get a memory model for  C++. RUST doesn't have a memory model yet -  it will probably have to get one one of these days.

Where does C++ need improvement?

C++ isn’t perfect. It would be great if it had a standardised package manager and there are a few other features we are working on for C++ 23. But it’s an evolving language that is always adapting to users’ needs and we have an annual conference just about the language. 

Bjarne Stroustrup

Bjarne Stroustrup

Creator @ C++

Bjarne Stroustrup is the designer and original implementer of C++ as well as the author of The C++ Programming Language (Fourth Edition), A Tour of C++ (Second edition), Programming: Principles and Practice using C++ (Second Edition), and many popular and academic publications. Dr. Stroustrup is a visiting professor at Columbia University. He is a member of the US National Academy of Engineering, and an IEEE, ACM, and CHM fellow. He received the 2018 Charles Stark Draper Prize, the IEEE Computer Society's 2018 Computer Pioneer Award, and the 2017 IET Faraday Medal. His research interests include distributed systems, design, programming techniques, software development tools, and programming languages. He is actively involved in the ISO standardization of C++. He holds a masters in Mathematics from Aarhus University, where he is an honorary professor, and a PhD in Computer Science from Cambridge University, where he is an honorary fellow of Churchill College.