As far as I've understood, one of the best practices to check system_error
conditions in a portable manner is to compare their code()
value with values in the std::errc
enumeration. However, when I try to run the following code, this does not seem to work.
#include <cassert>
#include <cerrno>
#include <system_error>
int main() {
try {
throw std::system_error(ENOENT, std::system_category());
} catch (std::system_error const & e) {
assert(e.code() == std::errc::no_such_file_or_directory); // <- FAILS!?
}
}
Do I misunderstand how these diagnostic errors are supposed to work, or am I doing something wrong? How should one compare std::system_error
exceptions to std::errc
values?
EDIT: The code seems to work properly using clang++
and libc++, but fails when building against libstdc++ regardless of which GCC or Clang compiler (and version) I use. Related to PR 60555? Any portable workaround?