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
- Miriam Castellanos
- Sections
- All eight
- Feed
- RSS
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.
written for C++20
How to write comparators for
std::sortandstd::mapstd::sort,std::setandstd::mapneed a strict weak ordering: the three rules, what breaks when one fails, and the comparison C++20’s<=>writes for you.written for C++20
C++ move semantics: what
std::moveand&&really doWhat a move is, why
std::movemoves nothing, what C++17, C++20 and C++23 changed about temporaries and returns, and four move mistakes clang-tidy catches.written for C++20
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.written for C++20
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.
written for C++20
std::expectedin C++23: return a value or an errorReturn 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.written for C++23
C++ map:
std::mapfind, insert, erase and iterateUse a C++ map: look a key up with find, contains or at, add one with
try_emplace, erase in a loop, and see whatstd::mapdoes when a key is missing.written for C++17
std::optionalin C++: how to use it and when not toCreate, test, read, empty and refill a
std::optional, what each accessor does when it is empty, what it costs, and whenstd::expectedfits better.written for C++17
std::printlnin C++23: print withoutstd::coutor printfstd::printlnin C++23: the syntax, eachstd::coutand printf line translated, what a bad format string does, whether it flushes, andstd::endlvs newline.written for C++23
std::spanin C++20: pass a view, not a pointer and a sizeTake
std::spanby value: what converts to it, slicing with subspan, what s[i] out of range does on each library, and how a span dangles.written for C++20