Saturday, March 26, 2011

Effective C++ in the C++0x (C++11) Age

Yesterday, Herb Sutter posted the truly exciting news that the new version of C++ has reached FDIS status.  Practically (though not officially) speaking, this means that the new standard for C++ is finished, and we can stop using the code name "C++0x" and start using "C++11" in its place.

I was recently informed that the German translation of Effective C++ will be reissued with a new cover as part of their "classics" series.  They asked me to write a little something about it, and I came up with a new preface that considers the relevance of the information in the book in the age of C++0x/C++11.  Given the adoption of the FDIS, plus the fact that the preface will be published only in translation, it seems appropriate to post it here:
2011 is an exciting year for C++ developers. It marks the official debut of the new standard for C++, informally known as “C++0x”. Over a dozen years in the making, C++0x makes C++ more powerful and flexible than ever. In addition to introducing major new features, such as support for concurrency, lambda expressions, move semantics, and type deduction for variables (auto variables), C++0x provides an assortment of secondary capabilities that also increase expressiveness, improve efficiency, and eliminate programming drudgework. Examples include range-based for loops, static assertions, inheriting and delegating constructors, reference-counting smart pointers, hash tables, regular expressions, variadic templates, and more. All told, the C++0x feature set is about twice that of “old” C++. Furthermore, the feature set is increasingly real: the number of C++0x features supported by modern compilers is large and growing quickly. 2011 isn't just the year when C++0x receives official ratification as a new standard, it's also the year in which it starts to see serious mainstream use.

Which may lead you to wonder whether the information and advice in this pre-C++0x edition of Effective C++ remains relevant. I'm pleased to report that it does. Surprisingly so, in fact. Having spent nearly two years steeped in the details of C++0x, I expected to groan a bit as I reviewed this book's table of contents with C++0x in mind. Surely some Items would be inappropriate. But the advice I found proved sound. Should C++0x developers prefer consts, enums, and inlines to #defines (Item 2)? They should. Should they prevent exceptions from leaving destructors (Item 8)? Certainly. Should they use objects to manage resources? Declare data members private? Consider alternatives to virtual functions? Factor parameter-independent code out of templates? (Items 13, 22, 35, and 44.) Yes, yes, yes, yes! My goal has always been for Effective C++'s table of contents to summarize the advice in the book, and that summary remains just as applicable to C++0x development as to “traditional” C++ development. C++0x is a bigger language, and in some ways it's a different one, but the core techniques for making effective use of “old” C++ are core for the effective use of C++0x, too.

This doesn't mean that this Effective C++ is a perfect match for C++0x. The book doesn't discuss features new to C++0x, so Items about making effective use of those features are missing. A C++0xified Effective C++ would certainly have Items devoted to move operations, to uniform initialization, and to lambda expressions, and it'd probably have an entire chapter on making effective use of the concurrency API. Such a book would also contain different examples, e.g., ones making use of auto variables, range-based for loops, in-class default initializers, as well as the occasional variadic template. To the extent that this book falls short in its support for C++0x, the errors are those of omission, not commission.

But Effective C++ has never claimed to be a comprehensive book. My intent has never been for it to address every aspect of the language. Rather, it has been to examine the most important aspects of C++ that affect practicing programmers on a day-to-day basis. C++0x introduces new aspects of the language that warrant discussion, but that doesn't diminish the importance of the issues I treat in this book. In fact, it may accentuate them, because effectiveness with C++0x requires mastery of the language it extends, i.e, “old” C++. As the shiny new parts of C++0x garner more and more attention, discussion of the “old” parts will recede, but those parts will still be important.

Whether you're programming in “traditional” C++, “new” C++, or some combination of the two, then, the information and advice in this book should serve you well, both now and in the future.


Anonymous said...

So does it mean for us: the new book is on its way?
or we have to wait until compiler manufacturers catch up first?
Perhaps series of articles?
Or even blog posts?

or shorter: when and how are you going to cover it?

-- Yuli

Anonymous said...

Sorry, seems like I found materials on the page:

Thank you!

-- Yuli

Scott Meyers said...

The thing about the Effective C++ books is that they can't be written until the community has figured out what it means to be effective in the language, and for the new features in C++0x/C++11, we don't know that yet. I'm doing my best to keep track of developments on that front, but for the time being, the best thing everybody can do to hasten the arrival of a legitimate new edition of Effective C++ is to publicize what they find is and is not effective about using the revised language. I'd like a fast-track to knowing what works and what doesn't as much as anybody else, but I don't know of a way to fast-forward experience.

Scott Meyers said...

Regarding my C++0x training materials referenced at the personal licensing page mentioned by Yuli, they offer a technical overview of many new features in C++0x/C++11 (it will take me a while to get used to just referring to it as C++11), and they contain some insights into effective use of those features, but they're not designed to be Effective C++ for C++0x/C++11. As I wrote in my last comment, we don't yet know how to be effective with the new features, but we're learning.

Pete said...

I like the training materials (will have to buy a copy), but will there also be a new book for C++11?

Perhaps "even more effective C++" ?

Scott Meyers said...

Not in the near future, at least not by me. Next year? Who knows?

Denis Shevchenko said...

Maybe "Most effective C++"? C++0x-features are really most effective in C++-history.

Unknown said...

If you do write a new C++0x book, it would be a dream comes true for me. I love your Effective C++ series. The way you described "walking over the broken glasses" made my eyes rolling.

Agileotter said...

I'm sure some time will be required to learn how to not overuse features, and a new book can come from that.

Maybe a new version of the book would look at solving the same problems using the new techniques/syntax where applicable.

Anonymous said...

How about C++ 0xA...?

as someone else suggested on another forum.


Unknown said...

Wouldn't that be C++ 0xB?

Piotr said...

It is July 2011 now and there's still no officiall standard yet. It may as well turn out to be C++0xC ;-)

Scott Meyers said...

The next standardization meeting is next month, and from all I've heard, chances of the standard being finalized at that point are very good. ISO bureaucracy may take a while after that, but I think C++0x is almost certainly going to turn out to be C++11.


Anonymous said...

Why is a "new" C++ needed?

Rather confused given that C++ seems to be dying here in North America?

Why these are linguistic features rather than say new libraries?

Here in Canada, calling the language new means its another way for an HR guy to screen your resume. Rather scary.

Peter said...

Hi Scott, love your books, glad to have found your blog!

I guess both your Effective books have covered C++11 to some extent since you've covered TR1 and Boost. Now it's just time to revel in the wonderful new keywords, features and possibilities!

/Avid C++-lover

Peter said...

Hi Scott, love your books so I'm glad to have found your blog!

I guess that your Effective books have already covered C++11 to some extend since you've included TR1 and Boost in your material. Personally I haven't really taken advantage of those libraries, so I'm really looking forward to delving into what ended up in C++11.

/Avid C++-user