Stroustrup on next-gen C++: I didn't want to let go of my baby
Badly taught, over-used... better than ever
C++ 11 is “far better than previous versions”, says the inventor of the language Bjarne Stroustrup. He was speaking at an online event marking the launch of Embarcadero's C++ Builder XE3, a rapid application tool targeting Windows and Mac OS X.
C++ Builder XE3 is a promising but curious product. Delphi and C++ Builder were successful because the tools offered fast native code performance and high productivity on Windows. Embarcadero is now striving to make them cross-platform tools, via a new framework called FireMonkey that supports Windows and OS X, with iOS and Android promised in 2013. To keep pace with C++ changes, Embarcadero has adopted a new 64-bit compiler front-end for XE3, based on the open source compiler front end Clang.
C++ is an ISO standard, first ratified in 1998 with C++ 11 completed in 2011, but Stroustrup revealed he was initially resistant to standardisation efforts.
“It took some arm-twisting to get me to realise that it was time to start a standards effort," he said. "People pointed out that you couldn’t have a language used by millions controlled by a single guy in a single company. Even if you could trust the guy, you can’t trust the corporation. I was a bit sad, because the things I wanted to do would take years instead of months, because you have to build up consensus, and then you have to wait for five compilers to catch up.
“On the other hand the fundamental argument is correct. If you want something that is really widely used, you need some kind of standard. As languages grow, if they’re owned by a single organisation, sooner or later they start taking on peculiar twists to support that organisation’s business plan. The allies of that corporation are favoured.
“A lot of the languages that are seen as competitors to C++ are owned by a single corporation, but they do tend to fight with all the other corporations, and portability is a really hard thing to achieve. It has always been difficult, but it is not getting any easier, and I think portability across platforms is a very big deal.
“When people get into these stupid language wars like what’s best C++ or Java, I can’t be that rude, since most of the major Java implementations are C++. Same with C#. Same with the browsers and such. It’s a big world and many languages are useful.”
Despite its wide usage, C++ is among the most complex programming languages and hard to learn. Stroustrup says the solution is not to attempt to learn everything, and that C++ 11 is easier than before.
“A lot of people look at C++ and want to understand every dark corner. This is what you do if you want to be a compiler writer, but most people should look at what is the easiest way to write a good solution for their problem," said Stroustrup.
“I try to focus on things that lead to correct programs fairly easily. I emphasise the use of constructors to establish invariance, to acquire resources, destructors for cleaning up, copy operations, and the new [C++ 11] move semantics that allow you to move an object out of one scope and into another one very cheaply," he added.
"One of the things that was limiting C++ 98 [the previous standard] was that you could build really good resource handles - like vector, or istream, or thread - but they are hard to move around because this is computers and everybody knows that you copy things. In the real world you don’t copy things.
"If I lend you my phone, I don’t make a copy of my phone, I move it. We can do the same in C++ now. If I make a 10,000 by 10,000 matrix in a function, I can hand it over to you at the cost of two pointer assignments. We are talking picoseconds. That is a huge difference in the way I can write code today.”
Stroustrup does not favour a "dumbing down" of the language. “People always want to simplify the language to do exactly what they want, but to be part of a huge global and multi-industry community the language has to support things you would never do in your field. I see the language as a general purpose tool, and coding standards as specifying what you can do in a specific domain,” he says.
According to Stroustrup, there are so many poor C++ tutorials out there that he decided to write his own, Programming Principles and Practice using C++ . "For about three weeks I was looking through C++ textbooks and had smoke coming out of my ears, muttering things like, 'If that’s C++ I don’t like it either.'"
"The quality of teaching C++ has gone down over the last 10 or 15 years. It's gone away from how to write a good program, to here is this long list of features you should understand. It is easy to teach a list of features, but hard to teach good programming.”
So what are programmers doing wrong? One thing is too much use of inheritance. "It is obviously hugely overused," he says. "There are languages where you can’t express yourself without inheritance - they fit everything into a hierarchy and it doesn’t make any sense. Inheritance should come from the domain, from the problem. It is good where there is an 'is a' or 'kind of' relationship in the fundamental domain. Shapes fit into this, there is something natural there. Similarly device controllers have natural hierarchies that you should exploit. If you forget about programming languages and look at the application domain, the questions about deep or shallow inheritance answer themselves.
He also takes care to distinguish "implementation inheritance, where in some sense you want a deep hierarchies so that most of the implementation is shared, and interface inheritance - where you don’t care, all you want to do is to hide a set of implementations behind a common interface. I don’t think people distinguish that enough."
Another bugbear is protected visibility. "When you build big hierarchies you get two kinds of users [of the classes]: the general users, and the people who extend the hierarchy. People who extend the hierarchy often need protected access. The reason I like public or private is that if it is private, nobody can mess with it.
"If I say protected, about some data, anybody can mess with it and scramble my data. That has been a problem. It is not such a problem if the protected interface really is functional, a set of functions that you have provided as support for implementers of new classes... The ideal is public or private, and sometimes out of necessity we use protected,” he said.
Macros are another issue. "For most uses of macros, there is something better in C++. The exceptions are source code control using #if and #ifdef. This will stay that way until we get some kind of module system. But I am not so worried about simple #ifdefs. I am worried about real macros that expand into code. They don’t follow any rules of C++ or any other language, they are text manipulation features. They make tool building harder, and they have delayed progress in C++."
Since code that includes thousands of macros is so difficult to maintain, Stroustrup suggests attempting to remove them from applications, automatically where possible, and manually where necessary. ®