Undefined Behaviour

-- What happens?

Let us run the code from the previous page: ned@lyta:~/outcome/build_posix$ bin/outcome-snippets_udts Segmentation fault (core dumped) ned@lyta:~/outcome/build_posix$ Apparently no exception was thrown? Correct. Nor was a value printed, though this will randomly vary depending on your compiler, libraries, the wind blowing on the day … You will probably find this surprising. This is because the default action for a user-defined error type is undefined behaviour1. Let us demonstrate this by recompiling the code to use the undefined behaviour sanitiser and see what happens instead: »

-- What happens?

Let us run the code from the previous page: ned@lyta:~/outcome/build_posix$ bin/outcome-snippets_error_code_enums1 Segmentation fault (core dumped) ned@lyta:~/outcome/build_posix$ Also undefined behaviour. This is because Outcome doesn’t know what to do with a strongly typed enum any more than some user defined struct. Let us register our error code enum with the C++ standard library. This causes Outcome to treat it quite differently. struct udt { int a{0}; explicit udt(int _a) : a(_a) { } udt() = default; int operator*() const { return a; } }; enum class err { success, failure1, failure2 }; // Tell the standard library that enum err is an error code enum // by specialising the is_error_code_enum trait. »