Posted on by & filed under C++, Tech.

By Brian Overland

For C++ programmers, maintaining legacy code has always been important. However, the last few upgrades to the specification have created what some experts have called “a whole new language.” So how do you start using it?

For your own projects, you may want to start using new features which improve your programs without breaking your old code. There are some excellent features that can be used this way.

1) Thousandth-Place Separator

Programs using big constants have always caused readability problems. For example, what number is this?

Can you tell at a quick glance what this is? A million? Ten million? A hundred million?

The C++14 specification supports a new place-separator (‘) that makes the identity of the large constant much more obvious:

Of course, anyone reading this in English would like to see commas used here instead, but the resulting syntax would be impossible. Consider this function call:

If a comma (,) were used as internal-place separator, would this statement call a function taking three arguments—10, 100, and 200—or would it take one argument equal to 10,100,200?

So the apostrophe (‘) works best. The nice thing is that you can start using it right away, even in old programs… old-fashioned literals such as “10000000” are still accepted as always, but for readability’s sake, should be revised whenever you can.

2) Range-Based “for”

If there’s a new compiler feature designed to potentially eliminate hours of debugging work, it’s ranged-based “for.” A grizzled old C programmer may prefer to use the old-fashioned “for” statement, and of course it’s still supported. For example, it’s typical to process array elements this way:

But anyone who’s been programming for a while can tell you that many bugs are created by setting border conditions incorrectly. It’s therefore safer—as well as easier—to process an array with the new “for” statement:

This just says, “For each element in array_of_flt, set that element to 0.0.”

This code is easier to type in. More importantly, it’s more error proof. It says to the compiler, “You figure out where the beginning and end of the array are, then go ahead and do the right thing.” There’s almost no reason not to go ahead and use such code on existing arrays. (Note that the reference operator, &, is necessary if you want the loop to be able to alter element values.)

One limitation is that the compiler must be able to determine the size of the array… and it isn’t very clever. Using range-based “for” is fine with arrays declared as global variables, as well as arrays processed in the same function in which they’re declared. But what you’d really like to do is pass an array from one function to another, while still being able to use ranged-based “for.”

Unfortunately, there’s no way to do that. I’ve tried. But with the STL collection classes, such as vectors, sets, and maps, you can. You can pass a vector from one function to another and the compiler can always determine the beginning and end.

This ranged-base “for” loop works even if the vector iVec is declared in one function and passed to another.

3) The auto Keyword

As long as you’re using range-based “for,” you ought to get in the habit of using the “auto” keyword as well. This keyword no longer has the meaning it once had in C… in which “auto” modified a variable declaration to say that such a variable was allocated on the stack. Such usage was, in practice, never necessary or useful.

Nowadays, the “auto” keyword declares a type determined by context. This is not a variable-type declaration; your loop variable will only hold one kind of data.

In the case of ranged-based “for,” it’s convenient to use “auto” instead of some complex type (assuming that someday you are using a complex type). But more importantly, there is no way “auto” can ever be wrong in this context. It will automatically be set to the base type of the container.

In this article, I’ve barely scratched the surface of the new features of C++14. But I’ve mentioned some that you can start to use right away without breaking legacy code.

About the Author

Brian Overland is the author of many books on C, C++, and Visual Basic. His latest book, “C++Without Fear” is available in Safari. He can be contacted at his personal site.

C++ Without Fear

Tags: C#, legacy,

Comments are closed.