Feeds

Bjarne Again: Hallelujah for C++

Plus: Now officially OK to admit you never used STL algorithms

Secure remote control for conventional and virtual desktops

Stob Now it has been scientifically demonstrated that we Delphi users are the happiest of all programmers, proper significance can at last be attached to the rare woes that occasionally break through our Stepford Coder personas.

My test case: Warren Postma, a Delphi-head, was recently forced out of his comfort zone to repair a C++ program. After a difficult encounter with a Boost library, he blogged himself of the grumpy assertion:

'The most awesome thing about Delphi is that it is not C++'.

This is a most ridiculous position. Warren was working with shared memory, an intrinsically tricky proposition. The Boost Interprocess library gives you absolutely the best possible shot at managing it cleanly.

An intricate, clever and graceful piece of work, the Boost code exploits some of C++'s most characteristic facilities to define a not-smart-but-tough pointer that tolerates being remapped into different address spaces. Any slack-jawed programmer (by which I mean me) can easily slap in some containers to make an ad hoc inter-process memory database in a few dozen lines of theoretically-portable code.

Here is C++ with its sleeves rolled up to expose magnificent biceps: calmly, confidently wading down into the mushy entrails of memory management with the power of a true systems language, yet bearing the application programmer safely on its broad shoulders, surrounded by the comforts of modern idioms and the expressiveness of current CompSci thinking. This is much more than wrapping a few API calls. You just aren't going to manage this in Delf, much less Java or C# or an interpretive scripting language.

... All that said, I do know what Warren means. Especially when 200 lines of angle-bracketed, colon-riddled gibberish falls on your head.

Anyway. The fourth edition of The C++ Programming Language (auth. Bjarne Stroustrup, pub Addison-Wesley, amzn pr £43.19, pp 1346) is out, and my! how it has grown.

Page count of The C++ Programming Language through four editions

Page count of The C++ Programming Language through four editions

The size of the book reflects the middle-aged sprawling of its subject. It accommodates the C++11 ISO standard, which pervades the painstakingly rewritten text throughout, down to the teeniest code example. So a line of code that introduces std::list in the previous edition

std::list<std::string> slogans;

is updated with a fancy, new-syntax initialiser:

std::list<std::string> slogans {"War is peace", "Freedom is Slavery", "Ignorance is Strength"};

This small fragment illustrates: 1) the author retains his playful-but-academic sense of humour (as well as Orwell quotes, I found lists of Monty Python team members, Cambridge colleges and philosophers; I looked in vain for a Girls Aloud reference), 2) the not-terribly-successful Third Edition's proportional, italic font used for code has been sacked and replaced by a much clearer sans serif font printed in blue, 3) this particular language feature - the curly bracket initialiser - is a boon not only for containers in general, but especially textbook examples using containers. Really. No need for pages of code filled with push_back() calls, to give a sort example something to chew on.

The Gadget Show

We must have our gadgets, we programmers. Here is a cornucopia, and I was soon flicking through the pages, greedily seeking out the new stuff. Ooh look, we have standards-sanctified threads, and lambdas, and regexs, and deduced type variable declarations, and the ghastly-looking, double-ampersanded 'rvalue' references. (These last are pre-doomed objects whose guts one is allowed to steal to save the expense of making a copy; whoever thought to label this concept with a '&&' has a very different way of thinking to mine.)

There's plenty for everybody, even for Delphi fans. Turns out, 20.3.4.1, that C++ finally has an override keyword (hurrah!). But it got in so late, they had to make it a "contextual" (ie second class) keyword, which means, instead of being up front like virtual, you have to stick it down the back of a function declaration, after the const (boo!). Furthermore, the plusplusPeeps can now declare a function with its return type dressed on the right, where it belongs:

auto main() -> int;

Notice that use of auto, which is crowned the chic keyword of this round of C++ increments; no longer the poor relation of static as per K&R, but a fantastic extension of the DRY principle to the declaration of variables, especially iterators. With auto one can both have and eat one's static typing.

“Are there readers out there who own great farms of mixin classes, yapping and baying from within their barbed wire enclosures? Do tell.”

A lot of this stuff is familiar. Like drinkers admitted before the pub officially opens, some - especially Boost aficionados - have been imbibing C++11 cocktails for many years.  It's all very well reading up on shared_ptr in the Visual Studio help pages or so-and-so's blog, but one still needs the Stroustrup Authorised Version. He's particularly good at articulating the intention of a feature, and constructing feasible examples. That remains the case in this edition as much as ever.

There again, because it is Dr S, and because there is a slight, defensive hint of Dr P-for-Pangloss about him, and perhaps because every feature must to some extent have his, Stroustrup's, dabs on it (he was at least the Patriarch in the Room when they voted it through), we don't see any of the less satisfactory features of C++ highlighted. One of the most useful (and, let's admit it, entertaining) sections of JavaScript: The Good Parts is the appendix where the blessed Douglas Crockford takes JavaScript's foibles outside and gives them a salutary kicking.

Nothing like this happens in The C++ Programming Language. There is a list of deprecated C++98 features: for example the stillborn exception specification mechanism, and the antiquated and baffling STL Algorithm helpers like bind1st() and mem_fun_t(). (By the way, I have negotiated with the ISO committee on behalf of all interested Reg readers, and it is now officially ok to admit in public that we never actually used the STL algorithms. Mind you, ever since the author of the standard text admitted he didn't either, what else could they do?) But all the rest is sunshine, or maybe light cloud cover.

click to enlarge

By comparison of editions, however, it is possible to detect what may be concealed editorial currents. I see that the big discussion of that old bogyman multiple inheritance has been buried deeper in the chapter in which it appears, and it is now preceded by an example which considers it in terms of the less controversial Java/C# model. (Does anybody really use it for anything else? Are there readers out there who own great farms of mixin classes, yapping and baying from within their barbed wire enclosures? Do tell.)

Meta Schmeta Programming

But if MI is on the wane, the star of C++'s generics has definitely been rising. Template metaprogramming (TMP) now gets two chapters to itself. TMP is a technique first demonstrated by Erwin Unruh who showed, as a sort of party trick, that it was possible to write programs in C++ that run during compilation - his example famously used compiler errors to display a sequence of prime numbers.

Put like that it sounds silly, but the possibilities are fantastic. The Boost Units library allows one to write code mixing with impunity engineering quantities recorded in, say, centimetres per second and cubits per solar day and still get the right answer in exactly the same way that the Mars climate orbiter didn't. A cut-down library based on the same principles is worked through in the text.

Unfortunately, because TMP is a discovered-not-designed approach, the language structures to support this technique are obscure, notwithstanding mitigating C++11 features like constexpr.

Stroustrup comments: 'Template metaprogramming attracts clever people'. I am deffo not arguing with him there. Personally, as a coder, I am content to get off at a foothills railway station named 'Light Template Halt', and let the rack-and-pinion train chug on without me up the gradient to 'Serious Meta Junction'.

Trouble is, this sort of opting out doesn't really work. These days, we must all use the clever template libraries, if only to handle our strings. When something goes amiss, dribbling dullards like me get showered with cryptic error messages and angle brackets, and we don't like it.

Which is where we came in, more or less. The new edition of The C++ Programming Language retains its predecessors' authority and completeness. Dr Stroustrup is to be congratulated on his revision, as the slightly typo-ridden first printing observes, 'much though [sic] have [sic] gone into its design' (P88). It carefully leads the reader through all the attractive, simplifying features of the new standard, as well as enumerating the complexities, old and newly hatched, for which the language is justly famous. All this in a framework of justifying essays and clarifying use cases and thoughtful explanations, and a whacking great reference to the standard library at the back.

To accomplish all this, C Cruft Cruft's best-known manual has swollen to huge dimensions. I apologise to Dr Stroustrup for repeating a trite thought, but I do wish we could get by with something smaller. ®

Providing a secure and efficient Helpdesk

More from The Register

next story
UNIX greybeards threaten Debian fork over systemd plan
'Veteran Unix Admins' fear desktop emphasis is betraying open source
Preview redux: Microsoft ships new Windows 10 build with 7,000 changes
Latest bleeding-edge bits borrow Action Center from Windows Phone
Netscape Navigator - the browser that started it all - turns 20
It was 20 years ago today, Marc Andreeesen taught the band to play
Google+ goes TITSUP. But WHO knew? How long? Anyone ... Hello ...
Wobbly Gmail, Contacts, Calendar on the other hand ...
Redmond top man Satya Nadella: 'Microsoft LOVES Linux'
Open-source 'love' fairly runneth over at cloud event
Chrome 38's new HTML tag support makes fatties FIT and SKINNIER
First browser to protect networks' bandwith using official spec
Admins! Never mind POODLE, there're NEW OpenSSL bugs to splat
Four new patches for open-source crypto libraries
prev story

Whitepapers

Forging a new future with identity relationship management
Learn about ForgeRock's next generation IRM platform and how it is designed to empower CEOS's and enterprises to engage with consumers.
Why and how to choose the right cloud vendor
The benefits of cloud-based storage in your processes. Eliminate onsite, disk-based backup and archiving in favor of cloud-based data protection.
Three 1TB solid state scorchers up for grabs
Big SSDs can be expensive but think big and think free because you could be the lucky winner of one of three 1TB Samsung SSD 840 EVO drives that we’re giving away worth over £300 apiece.
Reg Reader Research: SaaS based Email and Office Productivity Tools
Read this Reg reader report which provides advice and guidance for SMBs towards the use of SaaS based email and Office productivity tools.
Security for virtualized datacentres
Legacy security solutions are inefficient due to the architectural differences between physical and virtual environments.