3 ms·
There are numerous ways of handling this that are significantly better, for example using an std::optional yields this code: do_something().and_then(proces
by Kranar 5y ago
There are numerous ways of handling this that are significantly better, for example using an std::optional yields this code:
do_something().and_then(process);
Or structured bindings:
auto [error_code, result] = do_something();
if(error_code == 0) {
process(result);
}
- brandmeyer 5y agoIn the presence of types which are truthy (ie, contextually convertible to bool), its too easy to accidentally swap the unpacking: // Oops, backwards! auto [result, error_code] = do_something(); if(error_code) { process(result); }
- BoorishBears 5y agoTo me that's not easy... It's a very well established pattern to have (error, result) I'd immediately see something off with (result, error) And clang-tidy can catch this: https://clang.llvm.org/extra/clang-tidy/checks/readability-implicit-bool-conversion.html https://clang.llvm.org/extra/clang-tidy/checks/readability-i...
- Kranar 5y agoFair enough, in my codebase we always order data in terms of dependencies so that independent data comes before dependent data. In this case the result depends on the error_code, and the error_code is independent, so the error_code must be listed first.