Feeds

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

Raising the standard

  • alert
  • submit to reddit

Providing a secure and efficient Helpdesk

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

Internet Security Threat Report 2014

More from The Register

next story
UNIX greybeards threaten Debian fork over systemd plan
'Veteran Unix Admins' fear desktop emphasis is betraying open source
Netscape Navigator - the browser that started it all - turns 20
It was 20 years ago today, Marc Andreeesen taught the band to play
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.