An unthinking programmer's guide to the new C++

Raising the standard

  • alert
  • submit to reddit

Choosing a cloud hosting partner with confidence


So what’s all this about a new C++ standard, Verity?

Why you asking me? I know nussing. I’m down to write ‘Porcine influenza — a programmer’s perspective’ this month.

Don’t fib.

Oh very well. The enthusiasts imprisoned in the ISO C++ bunker are reputedly on the point of lighting a white smoke bonfire to indicate they have a new version of C++ ready. It’s provisionally called ‘C++0x’, where the ‘x’ stands for the year of its release.

This being 2009, there surely cannot be much doubt, can there? I mean, next year will surely be too late for an ‘0x’ standard?

Actually the C-plus-plusers point out it could be delayed until 200A, or indeed until 200F.

D’oh! How could I forget the joys of hexadecimal-based humour? No doubt about it, those C++ guys have still got it. But I guess this will be a conservative little standard, dotting a few semicolons and crossing the odd +?

By no means. This is to be a grand simplifying standard, making the old language accessible to dribbling dolts like you and me, who have so often struggled in the past.

Sounds wonderful, dribbling excepted. How about an example?

For example, what was the first bit of C++ that ever gave you trouble? Before you found out about multiple inheritance or default copy constructors or template template parameters?

Well, now you mention it, I never really got the hang of references. Couldn’t quite see why we needed them, what with the blessed K&R having bequeathed us pointers.

Exactly. You will be pleased to hear the new standard addresses your confusion head on. Whereas the old C++ let one write this

int& r1 = i; // i is an int

the new C++ one can do this:

int&& r2 = foo(); // foo() returns an int

Neat, eh?

I think you may have skimped a little on your research there, Verity. I’m pretty sure you were always allowed to call functions foo(), pretentious though it is in a Tech Model Railroad Club sort of way.

No, silly. Look at the declaration of r2. Look at the double ampersand.

Eughh! Cripes! I had noticed that but I thought that must have been some sort of typo that had unaccountably penetrated El Reg’s usually impeccable proof-reading system. So what does that mean? Some kind of reference to a reference, I guess? Somehow r2 is extra referency?



Well, I think it is really for passing big structures into functions. You know how those OUT parameter references you get in some languages, for when you need extra return values? And that have no meaningful value when passed in? I think these rvalue references are in some sense the opposite. You use them to make an IN parameter, but instead of being passed by value, it is a reference to a value that is about to die. You can trample all over it, because nobody will use it again. But it doesn’t cost loads and involve making copies as passing-by-value does.

I must say that was about as lucid as the text of a software licence agreement and half as amusing. Is there anything else to which you would like to draw my attention?

Ok, dig this new syntax:

int i = {}; // initialise an int

Don’t tell me. I can guess. It declares i and initialises it to the body of the first function that I thought of.

Nearly right. It means

int i = 0;

but brings the twin gifts of prolixity and obscurity to the party.

So why would they bother to… Oh, never mind. Bound to be some template tomfoolery. Presumably it works with pointers, like this:

int* p = {}; // initialise a pointer
// same as
int* p = 0;


Wrong. In the new C++, you have to write

int* p = nullptr;

Oh no. You are not getting me like that. That is what we used to write in C. These days you have to write a zero, or produce a letter from Andrew ‘Lookup’ Koenig letting you off.

That is not what we used to write in C. We used to write NULL in C. Do pay attention or I will send you back to learn your regex rhymes. Now, can you guess what this is?

auto i = 0;

Wait a moment. I have come across auto thing before. Isn’t it a K&R keyword meaning the variable declared in the common segment of a different other FORTRAN program?

Nearly. It means that you don’t have to bother to declare the int type — the compiler guesses it.

Bully for the compiler. Actually, I think I’d rather type int and save keyboard wear.

You can also use it to declare iterators and stuff

auto it = stlContainer.begin();

instead of

vector<int>::iterator it = stlContainer.begin();

You know, that’s about the first nearly practical thing you have mentioned. So if I write

auto i = {};

then it automatically initialises i to be the correct type and value, right?

Don’t be silly. Anyway, there is another use of auto I want to tell you about. You can use it to write functions the wrong way round.


Since time immemorial, curly brackets languages have let you make a function like this:

int someFunc() {
    // function body...

But in C++0x you can write

auto someFunc() -> int {
    // function body...

with the function’s return type supplied at the back of the function.

What? Why would I want to do that? That is completely mad. What is that, some politically correct adaptation of C++ for left-handed programmers or something? If I wanted to do that, I would code in Pascal.

It has many advantages. I believe it allows you to overcome C++’s most vexing parse. No, wait a moment, perhaps that’s the curly bracket initialisers.

I’ll give you a most vexing parse if you don’t look out. You are definitely deliberately winding me up now.

Absolutely not. There’s lots of other good stuff you must hear about.

You can stick in lambda functions anywhere you want, so that the appalling for_each template and its brethren suddenly become usable — as per my earlier petition — complete with a wonderful [square bracket] syntax for deciding which bits of local state will arise from the undead when the thing is called.

And you can make for loops in a style I last saw in CORAL 66, where one provides the list of values the loop variable takes. One, two, miss out three, five, six, now do four. And there are heaps of Boost library functions (of course), a scary new template programming concept called, um, ‘concepts’, a scary new template programming thing called ‘variadic templates’, constructors calling constructors, more fruitful unions…

You know, rather than bother you further, I think I will go read up on it for myself, if it’s all the same with you. Where can one find the dope on this?

Well, Bjarne Stroustrup’s site and the Wikipedia article aren’t bad places to start. Please shut the door on your way out. I want to concentrate on my new pet project. Nothing like the imminent arrival of a new C++ standard to encourage one to learn.

Erlang. ®

Beginner's guide to SSL certificates

More from The Register

next story
Be real, Apple: In-app goodie grab games AREN'T FREE – EU
Cupertino stands down after Euro legal threats
Download alert: Nearly ALL top 100 Android, iOS paid apps hacked
Attack of the Clones? Yeah, but much, much scarier – report
You stupid BRICK! PCs running Avast AV can't handle Windows fixes
Fix issued, fingers pointed, forums in flames
Microsoft: Your Linux Docker containers are now OURS to command
New tool lets admins wrangle Linux apps from Windows
Bada-Bing! Mozilla flips Firefox to YAHOO! for search
Microsoft system will be the default for browser in US until 2020
Facebook, working on Facebook at Work, works on Facebook. At Work
You don't want your cat or drunk pics at the office
Soz, web devs: Google snatches its Wallet off the table
Killing off web service in 3 months... but app-happy bonkers are fine
prev story


Why cloud backup?
Combining the latest advancements in disk-based backup with secure, integrated, cloud technologies offer organizations fast and assured recovery of their critical enterprise data.
A strategic approach to identity relationship management
ForgeRock commissioned Forrester to evaluate companies’ IAM practices and requirements when it comes to customer-facing scenarios versus employee-facing ones.
5 critical considerations for enterprise cloud backup
Key considerations when evaluating cloud backup solutions to ensure adequate protection security and availability of enterprise data.
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.
Getting ahead of the compliance curve
Learn about new services that make it easy to discover and manage certificates across the enterprise and how to get ahead of the compliance curve.