Without getting into the general exceptions vs error codes discussion, what do you think are the downsides of using std::pair or std:tuple for returning multiple values, namely the function’s return value AND the error/success code, similar to how many Go developers apparently do error handling?
This approach obviously has the advantage of not having to use out parameters for the function return value or error code (depending on which way around you prefer it).
The main downside of that simplistic (C level) approach to failure handling is a
I.e. there’s more that can go wrong, such as accessing an indeterminate result value. Or just using a return value when the function didn’t produce a meaningful one.
The old Barton & Nackman
Fallowclass solved this safety problem by restricting access to the result value. Essentially the calling code has to check if there is a result value, before using it, and using a logically non-existent result value causes an exception or termination. Theboost::optionalclass does much the same.If you don’t want a dependency on Boost, then an
Optionalclass is trivial to implement for POD result type, and at a cost of a little possible inefficiency (dynamic allocation) you can just use astd::vectorto carry a non-POD possible result.The challenge is to retain the clarity of the calling code, which is the whole point of the exercise…