Ask HN: How to refresh on modern features of C++?

101 points by ctb_mg ↗ HN
I first learned C++ 14 years ago and have only used it in a non-trivial fashion on one or two short projects since then.

Today, reading an article on popular C++ myths [1], I now realized there are a TON of things I didn't realize modern C++ can do now. auto, unique_ptr, for_each, and range-for loops were new to me. I'm not 100% certain if all of these were recent additions to the language.

What are some things I can do to brush up on the modern features of C++?

[1] https://isocpp.org/blog/2014/12/myths-1

62 comments

[ 9.1 ms ] story [ 92.2 ms ] thread
I've not read it, but the author's other books have been well-regarded for years:

http://www.amazon.com/Effective-Modern-Specific-Ways-Improve...

I've read it already, the final retail version, not the early draft and its pretty awesome (like all his other books).

IMO its better than the Bjarne Stroustrup book (which i've read too), but any of the two is fine to learn modern C++ features.

I agree. Effective C++ was/is the guide for C++98 - Effective Modern C++ is the guide for C++11.
I have read it. Would highly recommend it. Most other guides just go over the neat things, but this one also goes over all the potential pitfalls and things to watch out for (and it's C++ -- there's always something to watch out for :))
I would like to read this book but the O'Reilly website eBook + Print combo deal is not available until next year.. Does anyone know how I can order both and get the early access ebook now and then the print book when it's ready?
The first few chapters of The C++ Programming Language by Bjarne Stroustrup 4th edition go through a tour of C++11 that will quickly show you how to use modern C++ techniques. The rest of the book can be used as a reference to learn more about specific features.
Definitely this one, not only does he explains modern techniques he explains the rationale behind them, why they were added and how to use them without getting shot in the foot - emphasizing values over references and avoiding `delete`s.
Herb Sutter, one of the most prolific C++ experts out there, recently gave a talk on Modern C++ [1]. In the beginning of his talk, he states that every C++ developer should read a "Tour of C++" [2] by Bjarne Stroustrup, the original designer of C++.

I recently read it and it's great. The book has less than 200p, but still covers the most important parts of C++11, albeit not in great detail (the reader is pointed to "The C++ Programming Language" by the same author for that). Read it! And watch the youtube video in [1].

[1] https://www.youtube.com/watch?v=xnqTKD8uD64 [2] http://www.amazon.com/Tour-In-Depth-Series-Bjarne-Stroustrup...

cppreference.com is a good resource too. It's updated frequently.

http://en.cppreference.com/w/

I can't stand that site. I try to use the old SGI stl reference whenever I can.
it's a great site, but it's jarring at first. the sgi stl reference doesn't have a lot of c++11 in it.
whoa really? To me cppreference is the single best example of code documentation I've ever used. Clean, easy to search, thorough but skimmable listing of interfaces, great example code, modifiable example code, provides necessary information not part of function declarations (iterator invalidation rules). I'm surprised you prefer the far more bare bones SGI version.
How is the reference for a library abandoned over 15 years ago even relevant? (other than when researching history)
Bookmarking because I also plan on entering C++ soon. I really don't want to be too behind.
Similar story here, I played with C++ back in 1998 and then again in 2007.

After reading these two books it definitely changed the way I see and work with C++, I fell in love with it.

So reading old code-bases is very common, you still need to understand the old C++ anyway:

Professional C++, Wrox, Jan 2005

Then to get yourself up to speed with C++11 and C++14:

Professional C++ 3rd Edition - Wrox, Sept 2014

Just read the new edition of C++ Programming language by Bjarne Stroustrup ;)

Some things are hard to come by in any shorter sources. And at times you have to dig into the standard.

An example of rather obscure thing in C++ which changed in C++11 (sequencing): http://en.cppreference.com/w/cpp/language/eval_order

And another thing which can be pretty confusing without carefully reading the standard. Copy elision vs move constructors...

See http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2011/n324... § 12.8 (Copying and moving class objects), #32 - 33

Not to be disrespectful- but I want to learn more about C++ from anybody but Stroustrup. His writing (to me at least) always has the aspect of "nothing is wrong, and every compromise was picked correctly." I'd much rather read somebody like Meyers who will actually warn you away from some mal-features. And C++ is so large you really want a useful editorial view to any teaching.
From such books I liked "Thinking in C++" which is a good intro, but there is no up to date version and it's missing details.
Scott's style is also to be quasi-entertaining. In my opinion he uses this style of pointing out the dark corners not to inform but to make the reader/listener feel a part of the "epic struggle" / journey dialogue. It seems to work quite well to sell books, consulting and as a pedagogical conceit and is worth emulating.

Bjarne has never had such pretense and his writing also reflects his personal style. And he will tell you quite honestly that some distasteful aspects exist because C++ was a language created to be used (backwards compatibility is a feature, adding keywords/syntax only when necessary, etc) not that "nothing is wrong."

Any rational discussion of programming languages is one of relative merits, applicability to specific problems, and personal prefs, rather than one of absolute truths.

"And he will tell you quite honestly that some distasteful aspects exist because C++ was a language created to be used (backwards compatibility is a feature, adding keywords/syntax only when necessary, etc)"

Even that line reads as: C++ may have some less-desirable features, but no mistakes (everything was an optimally chosen tradeoff).

How about C++ Primer 5th edition?
The Scott Meyers books on Effective C++ are excellent.
I'm in the same boat and working through "A Tour of C++" by Stroustrup. It highlights the new features you're talking about with concise examples but doesn't flesh out the details. So you'll know that there's an often-better way to do certain things using the new language features. You'll need another reference for assistance with the details, often Stack Overflow examples suffice.

"Effective Modern C++" goes more into the details and is next on my queue.

I've done a bunch of Ruby, Java, and some Lisp programming since my last stint with C++. It feels like a whole new language.

Well at the risk of stating the obvious, there's a lot of armchair programmer style "read this", "watch this" instruction here. It's much better to do something concrete.

Go and do a trivial project in it, cppreference.com and cplusplus.com have c++11 tags on the new features. I'm sure you've had ideas recently and I'm sure the project is less trivial than you thought!

I think looking at language features first is "a solution looking for a problem" attitude.

People who learn C++ this way tend to miss stuff out, I think - there are a lot of thing that you can just muddle your way though without really understanding.

I'm sure there are some people that think this will be OK; after all, lots of people say you don't really have to understand templates unless you're writing a library (and so on). But I don't think it will be OK. There was very little in C++98 (that I knew well, at the time) that it was OK not to know; unless you knew all of it, pretty much, you couldn't really use any of it safely. (This is how Scott Meyers makes his living.) I can't imagine C++11 will be any different; I might not know it well, but I do note that it has many of the same people behind it.

obviously we disagree! i think it's ok to miss out on language features.

templates, as you say yourself you're ok not to know. you're also not ok not to know exceptions.

there's other stuff too, you can get away without the C preprocessor. C++ is too big to know all of and it's a waste of time to invest time trying to, unless you are on a standards committee or are writing a compiler...(and even then you can get away with being a specialist!).

it's much better to be a program writer than a language lawyer and dare I say it: more satisfying too.

i draw a huge distinction between people that understand a language and people that can use a language, and i think people underestimate the time and effort in understanding a language. i think that time and effort could be better spent writing programs.

I agree; sometimes you have to start somewhere. Then come back and improve knowledge. Trying to be a theoretical expert early on will bite you
The thing is that most of the things that your parent mentions are not just some theory. Exceptions, templates, and the C preprocessor are used in most real-life C++ code.

I think starting from somewhere with modern C++ means not starting at C strings, arrays, and bare pointers but starting with STL containers, STL algorithms, and smart pointers etc. to build good habits. But eventually any good introduction has to cover C arrays etc. as well.

Back when I started C++, Accelerated C++ was that sort of introduction. But AFAIK it's not updated for C++ 11 or 14.

I agree that picking a subset is the way to go. Google avoid two out of the three i mentioned that you take issue with avoiding.

Places I have worked the code is divided into libraries and coding standards per library are different to the whole. The point is to lazily learn the part that the codebase you are working on uses and get cracking rather than waste time getting book smart on parts you may never use.

unless you are on a standards committee or are writing a compiler...

Or you work on an existing codebase. Most C++ code that I worked on had templates, exceptions, and used the C preprocessor.

I used to have your attitude. This works for many languages, e.g. if you know some of the syntax of Python or Go and the usual stuff from the standard library, you can get pretty far.

If you want to write modern C++, this is not really true. You need to know about const correctness, lifetimes, some templates, etc. Or you cannot deal with most projects or libraries, like Boost, Eigen, etc.

It's like saying you can program Haskell in 2014 without knowing typeclasses, lenses, GADTs, etc. You can write some educational form of Haskell, sure. But many libraries use these.

Modern C++ is a moving target. By the time you get around to doing a project with one set of features they've been made redundant. Exceptions are a good example of this with noexcept etc coming in and replacing what went before.

I know nothing of haskell. My point is that you should learn the features of the codebase you work on, and it's a waste of time to learn all features, ultimately for a codebase you are not working on or may never work on.

incidentally, ditto, i used to have your attitude.

> obviously we disagree! i think it's ok to miss out on language features.

Sure, but there are a ridiculous number of edge cases which result in undefined behavior -- this is something that cannot be avoided when learning C++. You really need to understand what undefined behavior means and what precautions you need to take to avoid it. (Don't get me wrong, recent standards let you write code that's a lot less likely to run into UB, but you need to learn that from somewhere. You should definitely not be approaching modern C++ as an improved C!)

the edge cases are multiplicative, the more features you use, the more they interact. therefore by reducing language features that you use you reduce the number of edge cases you run in to. most shops agree on a subset of C++ before embarking on a project.

i didn't say to approach C++ as an improved C. you are more likely to learn undefined behaviour from compiler warnings than reading a merry array of 500 page books before sitting down to not finish a program.

unless you knew all of it, pretty much, you couldn't really use any of it safely.

Thank you for perhaps the best summation of everything wrong with using C++ in a project, and everything wrong with C++ as a language!

i also disagree with that statement, while amusing, it's untrue. you can use C++ without the things I have mentioned quite safely. in fact the less features you use, the less they interact and the safer the whole experience is.
You know what we call (C++)--?

C.

:)

you should actually preincrement there to avoid the copy constructor, the correct formulations is (++C)--
How do you know that my preincrement and postincrement are the same? ;)

Well played, at any rate.

I think you make a very valid point and I would agree to an extent. However I wouldn't discount the value in seeking to understand the personality and capabilities of a language as it evolves over time.

I think of any language as another tool in my toolbox. The better I know a tool, the better I know when and how to apply it to a specific problem.

I'd wager that just one trivial project won't fully demonstrate the personality and capabilities of a language.

absolutely, but I think acquiring knowledge by reading a 500 page book dutifully is a time sink. much better to get a touristic overview and solve actual problems rather than trying to get book smart.
I think the correct approach is a mix. I firmly believe that one of the things that makes good programmers good is developing the correct habits. Once something has been ingrained as a habit you don't even think about it any more and just do it.

I'd take a book like Scott Meyer's new Effective Modern C++ book (which I recommend) and focus on applying one item every week. At the start of the week read through the item. Every time you write code for that week make an effort to see if that item applies to what you're doing. Maybe read the item again midweek or towards the end of the week to see if the practical experience changes your view on what he wrote. The book contains 42 items so if you go through it like this you'd be done in less than a year.

I'm not saying that people shouldn't read the whole book at once. However, if you just read it through once and put it on the shelf I don't think you'll get the same benefit that you would get from spending time focusing on individual areas.

You may like to read and search the questions on stackoverflow. They have a lot of C++11 and C++14 questions and code examples and many of the contributors are very experienced with C++.

   http://stackoverflow.com/tags/c%2b%2b11/info
   http://stackoverflow.com/tags/c%2b%2b14/info
I use C++ almost daily. I love it. I've used it for many years now. Having said that, I try to avoid the newer standards unless I absolutely must use them.

Don't get me wrong. The new features are great. I love nullptr, fixed width integers, to_string, stol, etc. but I sometimes work in environments with very old compilers that don't offer these features. And when the only code you have is written for C++11 or newer, it can take a great deal of time and effort to make your code work on the older systems.

Also, if what you need is in the standard, then don't use any external libraries. There are a lot of great libraries out there (Boost, Crypto++, etc.), but they add complexity and build dependencies and can cause all sorts of support issues. If you add Boost only because you need to parse arguments, then you're really causing yourself and developers who come after you more trouble than it's worth. So every time you think you need an external library, think twice and talk it over with other, more experienced developers.

When I need more features than the older standard provides, I'll use a newer standard, but I strive to only use the exact features I need (don't go wild and convert your entire code to C++11). And as a very last resort, I'll use an external library (but only a mature, widely used one) when I cannot easily write what I need with std C++.

Hope this helps. And whatever you decide, I'm sure you'll enjoy working with C++.

I too worked with C++ for years but I haven't for some time. I plan to learn the C++11 stuff and get back up to speed because I hate the feeling of falling behind in a language that really has no competition in the niche it fills.

Here is a somewhat off-topic question for you: what is the job market like for C++? Is it mostly maintaining/enhancing legacy code or is there considerable new code being written in C++?

It's been just more than two years since I was paid to write software. I was a systems programmer. My knowledge is a bit dated. The C++ code I write today is to support IT security research (job) and my own personal interests (hobbies).

When I worked in systems programming (automobile systems research), C++ was very popular in the embedded/systems space. I believe it still is. Many systems people I knew were porting C to C++. Old code was maintained and new code was written. I'd say the break-down was about 50 50. In addition to adding C++ features, maintaining C compatibility was paramount.

I never worked with higher-level applications, but I'm sure that there's a lot of C++ up there too (web browsers, office suites, etc). Maybe even more.

I hate the feeling of falling behind in a language that really has no competition in the niche it fills.

Well, that's no longer true - Rust and Go are two contenders for C++'s "niche". They are still growing, but are already much nicer languages than C++. In some situations Erlang or Ocaml can also replace C++ successfully.

As much as I like Rust, it is in its infancy and it will be years before it gains similar traction, if it ever does. The other languages you mentioned are not in the same category (garbage collection alone excludes them).
Almost all of those new features are part of the C++11 standard so just get a reference that specifically says it has been updated for C++11. There is a list of C++ books on stack overflow.
I have read and I would highly recommend C++ Primer 5th edition. Although my purpose was also to brush up old(?) features of C++. It covers everything except multi-threading.

If you just want to skim - A Tour of C++ should suffice.

Start with QT libs.

I hate C++ with passion for almost two decades. But I recently started working on KDE sub-project and found it very nice.

A (maybe unrelated) question about companies like Google and Faceook, which are C++ powerhouses: how many (if any) of the C++11 or C++14 features have they approved for use in production code?
For Google open source projects, all C++11 features are approved except the following ones[0]:

    Functions (other than lambda functions) with trailing return types, e.g. writing auto foo() -> int; instead of int foo();, because of a desire to preserve stylistic consistency with the many existing function declarations.

    Compile-time rational numbers (<ratio>), because of concerns that it's tied to a more template-heavy interface style.

    The <cfenv> and <fenv.h> headers, because many compilers do not support those features reliably.

    Default lambda captures.
For Chromium, it's a completely different set of rules[1].

[0] http://google-styleguide.googlecode.com/svn/trunk/cppguide.h...

[1] https://chromium-cpp.appspot.com/

OSS projects would, presumably, have far fewer restrictions than internal revenue-bearing production use?
same with me, I am a very old c++ coder, learned during 2001-2002. Now I am doing TA at a graduate school, instructing a course on c++. It helped me to learn a lot of new stuffs on c++11.

"C++ Primer", by Lippman is the book that we try to follow.

If 2001-2002 is old, then what is 'used for 7 years starting 1994'?! :)

I will be curious to read the replies here, as a year ago I was considering different languages and environments for a cross-platform project, and ultimately C# (yes, C#) won out over C++. To be fair the requirements for the project meant that runtime performance wasn't so critical, but preventing the developer - myself! - from making mistakes was.

Maybe it is because of my age, but despite having lots of experience with C++ and a mostly pleasurable love-sometimes hate attitude towards it, I couldn't imagine it making first choice for me these days for new projects.

You want even MORE features in C++?. You have made Stroustrup happy.