CppCon 2018: Brand & Nash “What Could Possibly Go Wrong?: A Tale of Expectations and Exceptions”

CppCon · 13K views · 2018 · 1:02:41

Open on YouTube

C, C++, Rust, Go & systems languagesSoftware architecture & designtalk

Why Stack Overflow recommends it

Stack Overflow users link it when answering questions about C++ error handling and the tradeoffs between exceptions, error codes and expected-style return types.

Recommended by 1 people across 1 different months, 2019 to 2020.

What it is

A CppCon 2018 talk by Simon Brand (Microsoft) and Phil Nash on handling failure in C++. It compares exceptions, std::optional, the proposed std::expected, monadic chaining and syntactic sugar for error handling, and looks at a possible way to unify them.

What people said

Recommends einpoklum · 2020-07 · Stack Overflow · on “Is it good practice to return an error to the caller instead of throwing the error right away in C++”

There are different common practices w.r.t. error handling in C++ - as it is a multi-paradigmatic language. For example: Return status/error codes rather than results. Return results only, throw exceptions on error. Return value-or-error objects, such as std::expected [LINK] . Each of these has pros and cons. The most important thing is to be consistent in your program, and to coordinate with whoever calls your functions * - so that you meet their needs. For a detailed presentation of current options and a future potential alternative, see this talk by Brand & Nash at the annual C++ conference CppCon: CppCon 2018: "What Could Possibly Go Wrong?: A Tale of Expectations and Exceptions" [this video]

Cites t.niese · 2019-03 · Stack Overflow · on “double free or corruption when trying to erase in unordered_map”

@MichaelVeksler well exception handling has indeed its problems CppCon 2018: Brand & Nash “What Could Possibly Go Wrong?: A Tale of Expectations and Exceptions” [this video] , and a topic that is worked on. I don't think everything google write in the guidelines is valid/good, and you can argue about if raw pointer should only be used for optionals ( CppCon 2014: Herb Sutter "Back to the Basics! Essentials of Modern C++ Style" [LINK] ), but it is still not an anti-pattern.

Cites Elliott · 2020-02 · Stack Overflow · on “How can I iterate over a packed variadic template argument list?”

If your inputs are all of the same type, see OMGtechy 's great answer. For mixed-types we can use fold expressions [LINK] (introduced in c++17 ) with a callable (in this case, a lambda [LINK] ): #include <iostream> template <class ... Ts> void Foo (Ts && ... inputs) { int i = 0; ([&] { // Do things in your "loop" lambda ++i; std::cout << "input " << i << " = " << inputs << std::endl; } (), ...); } int main () { Foo(2, 3, 4u, (int64_t) 9, 'a', 2.3); } Live demo [LINK] (Thanks to glades [LINK] for pointing out in the comments that I didn't need to explicitly pass inputs to the lambda. This made it a lot neater.) If you need return / break s in your loop, here are some workarounds: Demo using t