Answers

Newest first. When a later standard changes an answer, the article is updated in place and says what changed and since when.

Each answer names the standard it is written for: 111417202326

Document
Answers
Answers
24, newest first
Page
2 of 3
Written by
Sections
All eight
Feed
RSS
  1. C++ generic lambdas, template lambdas and function objects

    Write generic lambdas with auto (C++14) and template parameter lists (C++20), and know when a function template or a named function object is the better tool.

    Templates §3.311 min readC++20

    written for C++20

  2. How to write comparators for std::sort and std::map

    std::sort, std::set and std::map need a strict weak ordering: the three rules, what breaks when one fails, and the comparison C++20’s <=> writes for you.

    Standard Library §2.36 min readC++20

    written for C++20

  3. C++ move semantics: what std::move and && really do

    What a move is, why std::move moves nothing, what C++17, C++20 and C++23 changed about temporaries and returns, and four move mistakes clang-tidy catches.

    Language §1.317 min readC++20

    written for C++20

  4. C++ pass by value or const reference: the rule since C++17

    By value, const reference, && or a view? Choose each C++ parameter by what the function does with it, with the C++17 rules and a program that counts copies.

    Performance §4.116 min readC++20

    written for C++20

  5. Rule of zero: the special members C++ writes for you

    Which special member functions C++ declares for a class, what declaring one does to the others, and when to write none, all five, or delete them.

    Language §1.413 min readC++20

    written for C++20

  6. std::expected in C++23: return a value or an error

    Return a value or an error with std::expected (C++23): std::unexpected, what each accessor does in the wrong state, chaining, choosing E, and when to use it.

    Standard Library §2.411 min readC++23

    written for C++23

  7. C++ map: std::map find, insert, erase and iterate

    Use a C++ map: look a key up with find, contains or at, add one with try_emplace, erase in a loop, and see what std::map does when a key is missing.

    Standard Library §2.511 min readC++17

    written for C++17

  8. std::optional in C++: how to use it and when not to

    Create, test, read, empty and refill a std::optional, what each accessor does when it is empty, what it costs, and when std::expected fits better.

    Standard Library §2.611 min readC++17

    written for C++17

  9. std::println in C++23: print without std::cout or printf

    std::println in C++23: the syntax, each std::cout and printf line translated, what a bad format string does, whether it flushes, and std::endl vs newline.

    Standard Library §2.77 min readC++23

    written for C++23

  10. std::span in C++20: pass a view, not a pointer and a size

    Take std::span by value: what converts to it, slicing with subspan, what s[i] out of range does on each library, and how a span dangles.

    Standard Library §2.810 min readC++20

    written for C++20