C++ 23 · Classes, memory and templates
Return a value or an error
std::expected holds a result or an error without exceptions (C++23).
std::expected<int, std::string> parse(const std::string& s) {
try { return std::stoi(s); }
catch (...) { return std::unexpected("not a number"); }
}
More classes, memory and templates commands
- Define a class
- Use polymorphism
- Own an object with unique_ptr
- Share ownership with shared_ptr
- Move instead of copy
- Lock a mutex safely
All classes, memory and templates commands · C++ 23 cheatsheet