Ask HN: Best way to learn modern C++?
Do you have some good advice or know any useful resources to learn in a proper way how to code in C++? For the context, I know some C, but C++ seems a different kind of beast, and it's easy to get lost in the huge variety of features and libraries it offers.
183 comments
[ 5.1 ms ] story [ 178 ms ] threadPeter Hintjens was writing “Scalable C” a couple years ago, but recently died, and never got very far unfortunately.
For OP: https://stackoverflow.com/questions/388242/the-definitive-c-...
Also some C++ related YT channels such as TheChernoProject, CppCon etc
Following the respective reddit and slack communities is also extremely helpful.
Also get your hands dirty with this: https://github.com/vitalets/github-trending-repos
or https://github.com/fffaraz/awesome-cpp There is more but this should do.
Oh I almost forgot: https://ishansharma.github.io/twitter-mentors/ http://stephaniehurlburt.com/blog/2016/11/14/list-of-enginee... https://twitter.com/sehurlburt/status/889004724669661184
Good Luck! :-)
For people who are coming from a classic C++ background, I find Bancila's Modern C++ Programming Cookbook to be a good first stop for discovering modern C++ idioms.
They are not free and prevent certain kinds of optimization as well as allow accidentally sharing state.
Of course it's a very good book though.
My recommendation is for "Accelerated C++" by Koenig and Moo. It's rooted in actual teaching experience and compared to most C++ books is pretty slim.
The only negatives these days is that it is a bit old and (still) expensive.
https://isocpp.org/faq (what Marshall Cline's excellent C++ FAQ has become)
and here:
https://herbsutter.com/gotw/ (Not as organized, but Herb Sutter wrestles with the essential questions of how to deal with the tricky bits of C++)
If you've got the cash, read Effective Modern C++ by Scott Meyers.
Finally, if you're ready to snark at all you've learned, read the C++ FQA:
http://yosefk.com/c++fqa/
You should check out Rust, it aims to replace C++ and do a better job in memory management, and developer experience in general (cross-platform compatibility, package management, etc)
No one can replace it, but everyone has to try.
Which is actually partially true... the long tail of programming languages today is very, very big. There's probably 10-50x more programmers now than there were back in 1995. The programming world can probably sustain 10-20 mainstream languages these days, if not more.
Don't know about that. But only yesterday afternoon I spent like 2 hours writing a Perl script to heavy lift some Unix file work, Which I factored would take a week at-least to do in any other language.
Perl is still largely a very useful language without any replacements. So are awk and sed.
Web development world seems to have moved on to something else now.
I'm guessing you're more familiar with Perl, because I personally find it hard to believe that Perl by itself is an order of magnitude more efficient to write than Python or Ruby.
Ruby is ok. But Perl is million times a better language than Python.
Python is an extremely overrated language.
Ada also had a shot, I think, but they screwed it up with no C compatibility and expensive SDKs - or something. For some reason, Ada is just not perceived as "cool". It actually seems like a pretty good language!
Joe Duffy mentioned on his Rustconf talk that Windows devs even with Midori running in front of them, it was still a very hard sell for them to psychology accept it as doable.
The problem with Ada was basically expensive SDK, Pascal-like syntax and not being part of OS SDKs.
Only systems programming languages that are bundled with OS SDKs survive on the market, others either get a niche (Ada - high integrity systems) or fade into oblivion/maintenance (Modula-2, Delphi, ...).
The main reason being, companies already payed for OS SDK or gotten it for free with the OS.
Any other language must add a very worthwhile set of benefits for them to look beyond the SDK and possible toolchain integration issues.
Hmm. To Rust, this would seem to say "get bundled or die".
In your view, is it enough to be a free download?
At very least, there needs to exist painless interoperability with the SDK toolchain, debugging tools, IDE, platform libraries.
Every piece of the puzzle that doesn't quite work like the SDK tools, means additional development/debugging effort.
Naturally the big question is always if the added benefit of using language X outside the expected workflow outweighs the extra development costs.
Just look at C++, it was on the path of being widely adopted by desktop OSes, every C compiler vendor was adding support for it.
Then BSD and Linux large scale adoption happened with GNU manifesto favouring C as the language to write portable software, Java and .NET came into the scene.
Microsoft was probably the only major OS vendor still caring about it, until LLVM happened and the wind started blowing again with C++11.
The embedded space is still an area where C++ is hardly seen, with a few conference talks last year discussing on how to advocate for it.
Now all major C compilers are written in C++, but had Microsoft focused only on .NET or LLVM never happened, and history would probably followed another path.
There were GC'ed operating systems in the 80s. The point is not this. It's writing applications where you can maximize your CPU usage.
If it's a game, it means that you want to write your code so that the highest number of objects are visible on screen and are being updated on each tick. If its's a multimedia software, you want to be able to run most sounds / videos in parallel at the same time. If it's an interpreter, you want to have the highest instructions-per-second count.
etc etc... the point is not to have "good enough" performance, it never has been as soon as you use non-trivial apps. It's always "how fast can it be" / "how far can I push it", and an implicit language-level GC puts limits to this.
The last generations of Oberon OS had support for video editing applications as an example.
There are real time GC applications in production being used to control weapons systems, including live targeting and soft real time factory automation systems.
Regarding D, using @nogc or -vgc allows the developer to control when the GC works, it at all. What the language lacks is a GC that can match real-time GCs used in soft real time systems.
For instanced I used it here to write Ada programs for the C API of the Pebble smartwatch: https://blog.adacore.com/make-with-ada-formal-proof-on-my-wr...
SIMD, GPGPU, GUI, embedded platforms, iOS, Android, UWP, COM, certified compilers, ...
For example,
https://www.iar.com/iar-embedded-workbench/certified-tools-f...
Maybe someone else can jump in.
However, I can tell you that in general proprietary compilers for embedded devices make Visual C++ feel state of the art, let alone when compared with gcc or clang.
Many of them are stuck in partial support for C99 or C++98 (e.g. TI), with no clear roadmap of future improvements.
Most compilers do a reasonable job with auto-vectorization, and if more is needed, there are instrisics.
No need to write straight Assembly.
Are the intrinsics standard? If so, what version of the standard were they in? How good is compiler support for them?
They are not portable between CPU architectures either way
For reference, C got real alignment support in C11 and C++ in C++17. Without this, even starting on SIMD is risky.
That said, you can go really far with a good C++ math library like Eigen which has singe vectorization in the operations themselves.
Regardless, yes, that’s the whole point. What’s been accepted is https://doc.rust-lang.org/nightly/core/arch/index.html , that is, intrinsics that map 1-1 to the vendor APIs. (I belive this is the same in C++ but not my area of specialty so I may be wrong)
https://doc.rust-lang.org/nightly/core/simd/index.html is the first level up the abstraction chain; this hasn’t gone through an RFC process yet, but will eventually happen.
On top of that, you have stuff like https://crates.io/crates/faster , which is very high level. Looks like they need to fix up the README... but you get the idea.
Also, autovectorization has existed this entire time. This stuff is for when that falls down.
To comment briefly on the learning curve, the trade off is basically, C++ is easier to get started, but harder on the high end. Rust is a bit harder for C people to get started, but then the high end is much easier. Rust’s curve is flatter, in a sense.
Oh and also: learning one will certainly help you learn the other in many cases. I advocate learning many languages :)
This week I’m visiting a ~700 person company in London that’s moving away from C and towards Rust. They’re not doing a Big Rewrite, obviously, so they still have quite a bit of C too. They chose Rust over C++ though. It always depends!
Especially the Rust borrow checker will make you mad in the first place, and then teach you something about memory management you will be able to use in C++. It really improves your mental model in places you didn't expect.
Rust is “move by default, move always means memcpy (and they often can be elided), no move constructors,” to (possibly too) succinctly describe it.
Then read Meyer's Effective modern C++.
And then have a local copy of cppreference.com (they provide a zip you can download) and set yourself some projects.
[1] https://en.wikipedia.org/wiki/Substitution_failure_is_not_an...
- Principles and Practice Using C++ (http://www.stroustrup.com/programming.html)
- From Mathematics to Generic Programming (http://www.fm2gp.com/)
- The Scott Meyers books
Some of the Bjarne Stroustrup videos,
"Learning and Teaching Modern C++" - https://www.youtube.com/watch?v=fX2W3nNjJIo
Some of the Herb Sutter videos,
"Writing Good C++14... By Default" - https://www.youtube.com/watch?v=hEx5DNLWGgA
"Back to the Basics! Essentials of Modern C++ Style" - https://www.youtube.com/watch?v=xnqTKD8uD64
Some of the Kate Gregory videos,
"Stop Teaching C" - https://www.youtube.com/watch?v=YnWhqhNdYyk
"10 Core Guidelines You Need to Start Using Now" - https://www.youtube.com/watch?v=XkDEzfpdcSg
"It's Complicated" - https://www.youtube.com/watch?v=tTexD26jIN4
It's a lot of the content in the above but crowdsourced, open for PRs, searchable, and readable from the browser.
I would also like to add that it helps to read "The Design and Evolution of C++".
I found that I plateaued early in my C++ journey. However, I was always reading from the experts and it really helped to read material from Dr. Stroustrup. Once I began to see the common thread behind what he was saying and understood why some things were the way they were, it helped understand when it's a good time to use/not-use certain things. This helped break past that barrier and to improve my skill further.
Also, it helped to write a _lot_ of c++ code and think about how that could be better.
I'm afraid I didn't read a tonne of good C++ code in that period but somehow, I managed to keep growing and at some point, it became my "go to" language and I really started to like it.
Long story, short: Read books from the experts, esp. Dr. Stroustrup.
I think that book is worth reading even to people who have no intention of going anywhere near C++, as long as they are interested in programming language design and evolution. I wish there were more books like this one to explain the kind of thinking that went into the design of a language and to tell its history.
I had commented on the book on HN about a month ago:
https://news.ycombinator.com/item?id=16275341
Excerpt from it:
"I found that book very interesting in many regards. I had bought and read it several years ago (out of interest, though I have not worked on C++ professionally).
Stroustrup goes into a lot of details about the reasons for many design decisions in the language."
That's one of the reasons that make the book interesting. He even goes into human and cultural aspects of programming.
They are truly great.
Not being a smart@$$ here. Any language you want to learn will only become natural when you've used it for a significant project or set of exercises. Either clone or port something non trivial, or use it for your latest pet project, or get permission to use it at work.
Also, the answer to this question depends a lot on your previous experience with other languages. If you're well-versed in another C-like language, jumping right in and starting to make something will probably teach you a lot, really quickly. If you're more of a beginner in programming generally, you need a book or a teacher.
I sometimes get carried away and stop reading more after I cross some threshold which prevented me from noticing lot of nice things like best practices that were happening in the industry/or better design patterns etc..
Not learning from books/teaching can make it hard to improve. I love reading other peoples code and thought processes because it often reveals how I'm missing something in my toolbox.
Studies have shown that one of the best ways to remember is to take a stab at a problem before learning the solution. So even quizzes on using libraries has helped me learn methods and concepts I would have not learned on my own if left to my own way of coding. I try a simple problem on my own before learning that there is a cleaner, more efficient solution. It's easier and more approachable to do that with small problems than trying to revise an entire document of code.
Lastly, I think a lot of experts underestimate the gap between novice and intermediate, where it's really hard and frustrating to attempt things that are a bit more complicated, requiring more background knowledge than just whats shown on the surface.
It's similar to why university lectures work best if the audience has already done its assigned reading. The people in the audience have better questions in mind while listening, so they can better intuit when a gap is filled in or if some part of the explanation doesn't make sense.
https://hn.algolia.com/?query=learn%20c%2B%2B&sort=byPopular...
For even more stuff - if you do the same search for C, especially 'modern C', you'll find those threads often also contain a parallel set of recommendation for C++ resources.
If you get interested on rendering Matt Pharr & Co's Oscar winning "Physically based rendering" is a didactic masterpiece. https://www.amazon.com/Physically-Based-Rendering-Third-Impl...
The book is one of the best examples of how to combine high quality code and academic exposition together.
I know that doesn't answer your question and to be honest, I am curious about the resources which come up here too, but C++ can be so painful to learn that I wanted to know what drives you into that direction. I mean, there are alternatives like Rust. Do you have a specific project in mind, need it to fulfill some résumé requirements or something completely different?
I find Rust is a much better language, but there are very few mature libraries right now if you do something outside the scope of what Mozilla is doing. Also very few companies that are working in Rust (AFAIK, if you know some let me know) looking for devs.
If you want to work in a company that is doing any kind of low level code you will either need C or C++ as a foundation. At least to understand existing code. Most companies won't just rewrite everything in Rust, no matter how good the language is.
I really hope Rust replaces C++ for new projects, but it's just too new to tell whether that will happen. I find there's a bit more momentum (and good ideas) than in D, but nobody can predict the future from 2-3 years of language history.
“This Week in Rust” lists explicit job postings. One of the latest companies looking for Rust programmers is a little company you may have heard of: Facebook.
That said, many places are hiring good programmers and putting them on Rust projects, rather than hiring Rust programmers explicitly. There aren’t many pure Rust shops yet, so they need a broader skill set.
I still haven't grasped Rust as much as I'd like, but I have to admit that at least it feels like every part of the language fits nicely together with the others, as opposed to the "messy spaghetti" feel I get from C++ (though this is of course my subjective impression).
There are things I'd like they do, like removing the Python 2 dependency for building from source, or actually writing a spec. But I guess that'll come with time.
Any complex or high-performance computing relies on C/C++ pretty heavily still.
It's also a good tool to have in your box for building well-performing cross-platform native apps with Qt.
I say this as one who's probably at the same stage as OP. Know some C, and reasonably comfortable with the basics. Application of C++ is a little more widespread outside of strict embedded environments, however. (Another space I personally want to immerse myself in)
(There are worse but deprecated like MFC. Or on par like wxWidgets.)
I'd like to learn OpenGL programming, and C seems too painfully low level for that, although I won't argue that it is a lot easier to wrap your head around it. However, this days I've been toying with C++14, and the experience (apart from the dependencies, Makefiles, et al. mess, has been pretty smooth with regards to syntax.
Apart from OpenGL (which seems like a neat way to learn some interesting maths and physics), learning C++ helps delve into many big and important existing codebases, especially databases, and other complex systems. Rust may be designed for that but it isn't used so much in the wild.
Moreover, I want to feel badass in a hardcore low-level hacker way after programming so much in high-level dynamically-typed languages, and contribute my one stone to the future grave of JS/Node (let's hope Atwood's law won't work :P).
Additionally while traditional and more detailed books already posted here I'll just add one I personally liked: Discovering Modern C++ by Peter Gottschling. It's had exactly information I would expect be needed for person who already have reasonable experience in programming, but want short introduction into C++.
Disclaimer: I'm certainly not someone you can call experienced C++ programmer since I just developing and manage open source project for 3+ years. So take my opinion with a grain of salt.
Personally I started doing this then never went back. Seems most of the jobs out there for C++ really aren't that interesting.
In terms of jobs, C++ is used heavily, if not the majority of the time in: AAA game development, high performance computing, high frequency trading and quantitative finance, embedded software, image processing and computer vision... honestly, I focused on C++ out of school years ago BECAUSE of how interesting many of the jobs are. They’re also generally more stable and thus competitive over time compared to e.g. web dev, enterprise software, and so on, because the domain knowledge required means you’re a lot more valuable than knowing X and Y design pattern or framework Z.
http://www.informit.com/promotions/programming-with-c-and-c-...
The biggest message seems to be “don’t treat C++ as C with extra stuff, it’s its own language.”
That's true, but IMO it's useful to study the standard library to understand what it offers, as opposed to examples of well-designed C++ code.
People coming to the language lack the expertise to tell well-designed code from bad-designed. It’s tempting to treat the whole standard library as an example of the former.
Maybe that’s a personal thing but I remember doing that when I just started my career back in 2000. STL wasn’t quite there but MFC was already in wide use. Because I lacked the experience I considered whatever’s there is good, and ended up using not so good parts such as C++ exceptions and macro-driven dynamic serialization.
That statement isn't true for (at least) me. The main reason is that I'm usually working on code that must be compile-able as C++11, and/or may require an old-ish version of gcc's standard C++ library.
So in my daily work I usually have little/no reason to interact with the newer parts of the standard library.