I'm aware that error_code is system-dependent and error_condition is system-independent, but does this mean they will be different if we specify the value and category when constructing them. For example:
std::error_code ecode(1, std::system_category());
std::error_condition econd(1, std::system_category());
if (ecode == econd) // is this condition always true no matter what platform we are in??
the above is true in XCode in macOS, so I'm wondering if it's always the case if we are in other platforms, e.g. Windows.
If so, why would this be the case given ecode is system-dependent and econd is system-independent?
They are not. The equality of error codes and conditions is determined by the category member function "equivalent", and you could write a category that never makes any codes and conditions equal. For example:
#include <system_error>
#include <iostream>
struct cat_type : std::error_category
{
const char *name() const noexcept { return "name"; }
std::string message(int) const { return "message"; }
bool equivalent(int, const std::error_condition &) const noexcept { return false; }
bool equivalent(const std::error_code &, int) const noexcept { return false; }
} cat;
int main() {
std::error_code ecode(1, cat);
std::error_condition econd(1, cat);
std::cout << (ecode == econd) << '\n';
}
This program prints 0 because each overload of equivalent is called and both of them return false, so they are not equal.
However, for std::system_category specifically the standard requires that the equivalent functions have the default behavior (See N4800 section 18.5.2.5 syserr.errcat.objects paragraph 4), and since the default behavior is to consider codes and conditions with the same category and value equal, they will compare equal.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With