The New C++: Lay down your guns, knives, and clubs

Pick up your multI-cores

"The world is built on C++," Herb Sutter tells The Reg. Considering he's one of the language's chief stewards, we question his impartiality. But he does have a point.

After more than 30 years – depending on whose numbers you swallow (these or these) – C++ remains one of computing's most popular programming languages, favored for its speed if not its simplicity.

Apple's Mac OS X, Adobe Illustrator, Facebook, Google's Chrome browser, the Apache MapReduce clustered data-processing architecture, Microsoft Windows 7 and Internet Explorer, Firefox, and MySQL – to name just a handful – are written in part or in their entirety with C++.

And while new languages have since been born, C++ has endured. Java is the only one to have outpaced it in popularity, according to the TIOBE Index. PHP, Ruby, and JavaScript? Sure, some might claim that they are the future, but C++ is the past, the present, and the future.

According to Sutter, C++ is on the verge of its biggest change in the 13 years since it became an official ISO standard, a change that will make it relevant for the next two decades.

The recently finished C++ ISO standard, with the working name of C++0x, is due to be published this summer, following the finishing touches to the ISO spec language and standards wonks agreed upon in March.

Sutter didn't create C++, but he is chair of the ISO's C++ committee in addition to being a noted C++ programmer, author, and chief native-languages architect at Microsoft.

Sutter's world: C++ owns it

Sutter tells us: "This is the first major rev of the standard with new features since 1998." when the ISO ratified the first C++ standard.

The new C++ features many changes, but when asked to name the biggest, Sutter highlights those that address some of C++'s biggest bugbears: productivity and efficiency.

The complexity of working in C++ is one thing that Sun Microsystems' James Gosling tried to tackle when making a language that was more programmer-friendly; he cooked up Java, reputedly calling it C++ "without the guns, knives, and clubs." C++'s complexity might also be responsible for making C++ jockeys more prone to swearing than other coders.

Sutter lists auto keywords, lambda functions, and initializer lists as reasons you'll type – and swear – less. Some of the C++ clutter has also been stripped out.

"It really feels like you get the familiar power and way of talking about your code, but [it] feels like a fresh language that doesn't get in your way ... and [you] can write in much more expressive language," Sutter tells us.

Arguably the most significant changes will help C++ catch up to Java in popularity, now that C++ software can be built and run with fewer headaches in the rapidly dawning world of multi-core CPUs.

Without a memory model, devs have either been building or picking their own libraries to achieve concurrency. Concurrency goes hand in hand with multi-core, as it means that chips with more than one core can run different threads in an application across their cores – or even on different servers. It helps handle the way the threads talk to the chip's memory.

The need for a consistent memory model is growing as more multi-core chips from Intel, AMD, and others become standard in mainstream computers from the PC on your desk to servers at Facebook to the smartphone in your hand.

Single-core eaten up

Sutter firmly believes that C++ must go concurrent, as the free lunch of single-core is over.

Sutter says that now is the time to establish concurrency in the standard, to make sure C++ runs efficiently and consistently everywhere. "You can [do], and people have done, just about anything in C++. The question in the standard is: 'What are you able to do that's portable?'" he says.

"The question is how have [data center and web] environments become standardized and formalized that it's time to standardize one common way of speaking to them and express those programs. Have things quieted down enough that now is the time to say: 'Here is the way'?"

The memory model means that C++ code now has a standardized library to call regardless of who made the compiler and on what platform it's running. There's a standard way to control how different threads talk to the processor's memory.

"When you are talking about splitting [code] across different cores that's in the standard, we are talking about the memory model. We are going to optimize it without breaking the following assumptions people are going to make in the code," Sutter said.

It's a small world, and more of it

Those assumptions? "It's a contact between the programmer and the optimizer of the system, to run across cores and different memories as long as we make the following assumptions the programmer makes," he says, "and on the programmer side, as long as he uses features in the right places, he's guaranteed not to tell the difference that the optimization has not changed the meaning of the program. That's what you need to have a usable programming language and still be fast and optimize."

Multi-core comes in chips, big and small, and is starting to make inroads into smartphones and tablets for the mainstream consumer. The iPad 2 in March was the first iOS device to use a multi-core processor – the dual-core 1GHz Apple A5. For systems built on Android – the fastest growing smartphone operating system, according to analysts – Texas Instruments, Qualcomm, and Motorola have gone dual-core, as well.

One factor that's helped C++ is its performance: as a native language, C++ talks to the CPU without going through an interpreter, unlike Java or even Microsoft's .NET, so it's generally considered fast – and performance is vital on devices such as tablets and smartphones.

According to Google, C++ is the fastest runtime language when compared to Java, Scala, and Go, although it does require more extensive tuning and more expertise to achieve such results.

No guns, knives, or clubs: Gosling's take on C++ versus Java

The rise in mobile devices means new opportunities for the new C++. For example, Google introduced the Android Native Development Kit (NDK) for use with the SDK to build "performance-critical portions of your apps in native code" – meaning C and C++.

An NDK update introduced in March 2010 supports OpenGL ES 2.0 and 3D elements, meaning Google threw open graphics for things like games to native C++. Apple's iOS for the iPhone and iPad, meanwhile, runs Objective C in addition to C++ and C.

According to Sutter, the combination of multi-core and native code like C++ will let devs squeeze the most out of the next generation of hand-held devices. "Managed languages like Java and .Net are still great, but the performance you give up when you use them is more important," he says. "Look at smartphones – almost every first release says 'managed only', but version two says 'native is important'. We want to wring the performance out of them."

The language may be fast, but the discussions that created the new C++ haven't been. Work on C++0x began in 2003, and took eight years to complete. You might ask how the standard can be relevant for both web-scale operations such as Facebook and palm-sized multi-function computing such as the iPhone, seeing as how work began on C++0x a year before the first Facebook page and four years before Jobs & Co. birthed the first iPhone.

Sutter answers that doubt by saying that the changes in C++0x are timeless, and part of an evolution affecting all languages, not just C++. "A lot of these [changes] are related to things that aren't moving targets," he says. "The 2003 proposals have been, and will be, useful for another 20 years."

He also reckons that the new C++ is ahead of the game because today's generation of C++ compilers lack the features needed to thoroughly milk what's in the new spec. In many ways this is a repeat of what happened when the standard was set in 1998. "We did nothing in the language to let compliers catch up, because [C++ in 1998] was ahead of where most compilers were. In 2003 we started again. Eight years later, we have a standard that most compliers don't support yet, but it's much closer," he says.

That said, Sutter believes that the version of the spec that will come after the one published this summer will be delivered more quickly. There won't be a quiet period of several years, he promises, saying that those attending the final C++0x meeting in March have begun pushing to start work on the next version.

"It'll be sooner rather than later," he says a little enigmatically.

Quicker, cleaner, Java-ier

And there could be more features from the world of Java added. Sutter expects garbage collection to be proposed – it was dropped from C++0x due to a lack of time. The Java Virtual Machine has had garbage collection for years. C++ fans can be touchy on this subject, saying there are lots of different ways to do garbage collection – but that's the problem.

In Sutter's world, the time has come to update the official standard to make sure that C++ software runs as efficiently as possible and in the same way on different platforms and chips. With a fresh proliferation of smartphones and tablets running a new wave of operating systems and chips on different form factors, that focus would seem to be right.

Interestingly, C++ was ratified as an ISO standard just as Java failed to get ISO pick-up after a campaign by Sun. Sun came up with the Java Community Process (JCP), to police compatibility as a step towards realizing the dream of "write once, run anywhere".

"C++ has for 13 years been an ISO and ANSI standard language – that defined the portable part," Sutter says. "Now it's time to extend that. ... You wouldn't add to the standard to extend it further if the language isn't used. That's the only reason you wouldn't want to make it stronger."

But as we know, C++ is far from unused. The world is programmed on it. ®