In error_code.hpp you can find this definition:
class error_code { ... typedef void (*unspecified_bool_type)(); static void unspecified_bool_true() {} operator unspecified_bool_type() const
If you use something like this:
if (!ec) { // no error }
you get the right behavior, I hope this is clear. When you call this:
if (ec) {
you are actually calling operator unspecified_bool_type() because it returns a pointer (for a function) and can be converted to bool. If there is an error, it returns a pointer to unspecified_bool_true , which is not null. Therefore, it works correctly and does not solve the problem.
source share