

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

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.<p>Today, reading an article on popular C++ myths [1], I now realized there are a TON of things I didn&#x27;t realize modern C++ can do now. auto, unique_ptr, for_each, and range-for loops were new to me. I&#x27;m not 100% certain if all of these were recent additions to the language.<p>What are some things I can do to brush up on the modern features of C++?<p>[1] https:&#x2F;&#x2F;isocpp.org&#x2F;blog&#x2F;2014&#x2F;12&#x2F;myths-1
======
jerv
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](https://www.youtube.com/watch?v=xnqTKD8uD64)
[2] [http://www.amazon.com/Tour-In-Depth-Series-Bjarne-
Stroustrup...](http://www.amazon.com/Tour-In-Depth-Series-Bjarne-
Stroustrup/dp/0321958314)

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

[http://en.cppreference.com/w/](http://en.cppreference.com/w/)

~~~
lallysingh
I can't stand that site. I try to use the old SGI stl reference whenever I
can.

~~~
forrestthewoods
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.

------
tjr
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...](http://www.amazon.com/Effective-Modern-Specific-Ways-
Improve/dp/1491903996/)

~~~
overgard
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
:))

~~~
azmenthe
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?

------
vowelless
Watch the videos from GoingNative and CppCon. Speakers like Bjarne and Herb
constantly talk about "whats new in Modern C++". Watch Andrei's talks for
sure!

[http://channel9.msdn.com/Events/GoingNative/GoingNative-2012](http://channel9.msdn.com/Events/GoingNative/GoingNative-2012)

[http://channel9.msdn.com/Events/GoingNative/2013](http://channel9.msdn.com/Events/GoingNative/2013)

[https://www.youtube.com/user/CppCon](https://www.youtube.com/user/CppCon)

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

~~~
inglor
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.

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

~~~
to3m
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.

~~~
throwawayaway
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.

~~~
danieldk
_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.

~~~
throwawayaway
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.

------
danieljh
If you want to learn by examples, check out the following repositories:

[https://github.com/daniel-j-h/cpp14-snippets](https://github.com/daniel-
j-h/cpp14-snippets) [https://github.com/daniel-
j-h/cpp11-snippets](https://github.com/daniel-j-h/cpp11-snippets)

(Disclaimer: the C++14 version is still missing proper references to the
official C++14 standard)

------
pmelendez
As other said, Meyer's book is highly recommended.

Also, this talk would give you a very general overview of the new features of
the language:

[http://channel9.msdn.com/Events/GoingNative/2013/Opening-
Key...](http://channel9.msdn.com/Events/GoingNative/2013/Opening-Keynote-
Bjarne-Stroustrup)

Another book that can be a refresh introduction would be:

[http://www.amazon.com/Tour-C--
Depth/dp/0321958314/ref=sr_1_1...](http://www.amazon.com/Tour-C--
Depth/dp/0321958314/ref=sr_1_1?ie=UTF8&qid=1419355516&sr=8-1&keywords=tour+of+c%2B%2B)

------
w8rbt
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++.

~~~
cgh
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++?

~~~
dreamer_
_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.

~~~
cgh
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).

------
thepumpkin1979
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

------
acqq
Go to the Stroustrup's site, the page with the contents of his "C++ 4th ed"
book contains even the full drafts of the "Tour of C++" part of the book.

[http://www.stroustrup.com/4thContents.html](http://www.stroustrup.com/4thContents.html)

------
shmerl
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](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...](http://www.open-
std.org/jtc1/sc22/wg21/docs/papers/2011/n3242.pdf) § 12.8 (Copying and moving
class objects), #32 - 33

~~~
jmount
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.

~~~
shawn-butler
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.

~~~
jmount
"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).

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

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

------
discardorama
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?

~~~
desdiv
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...](http://google-
styleguide.googlecode.com/svn/trunk/cppguide.html#C++11)

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

~~~
discardorama
OSS projects would, presumably, have far fewer restrictions than internal
revenue-bearing production use?

------
wglb
The Scott Meyers books on Effective C++ are excellent.

------
thdn
Take a look at Stephan T. Lavavej aka STL series at C9,
[http://channel9.msdn.com/Series/C9-Lectures-Stephan-T-
Lavave...](http://channel9.msdn.com/Series/C9-Lectures-Stephan-T-Lavavej-
Standard-Template-Library-STL-)

Totally worthy.

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

~~~
mb_72
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.

------
gdy
Don't like Scott Meyer's books, but this pdf has chance to be good:
[http://www.artima.com/shop/overview_of_the_new_cpp](http://www.artima.com/shop/overview_of_the_new_cpp)

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

------
sedeki
How about C++ Primer 5th edition?

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

------
lyhkop
aaa

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

