top | item 32881087

(no title)

OliverM | 3 years ago

C++ was one of the first languages I learnt as an undergrad, maybe 20 years ago. I used it for a few hobby projects but not much else (professionally these days I mainly use Typescript & Go). I'd love to pick up C++ again but I've found it really challenging to discover what I should read up on to know what modern C++ should look like. What's a great resource for someone who's been out of C++ for a couple of decades to pick up and learn today's idiomatic C++? I don't want Rust btw - it's a great language but I want to revitalise my C++ knowledge, not jump ship entirely.

discuss

order

raegis|3 years ago

I think Jonathan Boccara's talk, "105 STL Algorithms in Less Than an Hour" ( https://www.youtube.com/watch?v=2olsGf6JIkU ), gives a nice overview of the <algorithm> library. Also, the examples on the reference pages (usually near the bottom) at cppreference.com are usually pretty good. For example: Here's how to create a pseudo random number generator using the Mersenne Twister engine.

  std::mt19937 e; // Mersenne Twister engine
  std::uniform_int_distribution<int> u(0,99999); // uniform ints 0 to 99999, inclusive
  auto rng = std::bind(u,e); // so rng() retuns the next random number
And filling a vector with random numbers could look like this:

  std::vector<int> nums(1000);
  std::generate(nums.begin(), nums.end(), rng);
This last line can be written in C++20 more simply like this:

  std::ranges::generate(nums,rng);

cyber_kinetist|3 years ago

Why should I do this, when I can just write:

    for (int i = 0; i < nums.size(); i++) {
        nums[i] = rng();
    }
Simple, easy to understand, actually compiles to efficient code even in Debug mode, don't need to include <algorithm> header which includes 10000+ lines of STL code which bloats compile times, etc.

detaro|3 years ago

The standard recommendations to come to up speed up to ~C++14 are Scott Meyers books, Effective C++ and Effective Modern C++, but I'm not sure if they work well alone when you've not used C++ in a long time. And don't have a good recommendation for something covering the stuff newer than that.

rramadass|3 years ago

* Discovering Modern C++ by Peter Gottschling - Good fast coverage of the features.

* The C++ Programming Language, 4th edition by Bjarne Stroustrup - The biggest change in the language was the introduction of C++11 and this book covers it well.

* A Tour of C++, 3rd edition by Bjarne Stroustrup - Summary of all changes upto C++20.

* Software Architecture with C++: Design modern systems using effective architecture concepts, design patterns, and techniques with C++20 by Adrian Ostrowski et.al. - More like a broad survey rather than in-depth but covers development in a "modern ecosystem".

* Modern C++ Programming Cookbook: Master C++ core language and standard library features, with over 100 recipes, updated to C++20, 2nd Edition by Marius Bancila - A series of articles one each for a language feature.

ncmncm|3 years ago

Generally Cppreference.com has the most authoritative information on what C++ is now. It is somewhat opinionated about modernity, but not enough so to be intrusive.

Usage examples have often been updated to current best practice.