Original URL: https://www.theregister.com/2009/05/07/verity_stob_cplusplus/

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

Raising the standard

By Verity Stob

Posted in Software, 7th May 2009 11:16 GMT

Stob

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?

Nope.

And?

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;

Right?

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.

Um…

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. ®