如果我调用一个通过GetLastError报告错误的Win32函数,例如RegisterClassEx,我该如何为该错误抛出std::system_error?
std::system_category()
类别中的std::error_code
通常无法与std::errc
枚举常量配合使用。例如:
std::error_code(ERROR_FILE_NOT_FOUND, std::system_category()) != std::errc::no_such_file_or_directory)
某些供应商需要使用std::generic_category()
才能正常工作。std::error_code(ERROR_FILE_NOT_FOUND, std::system_category()).message()
将无法给出正确的消息。std::system_error
和std::system_category
子类化为windows_error
和windows_category
并自行实现正确的功能。YMMV,据报告,VS2017可以按预期工作。ERROR_FILE_NOT_FOUND
。其次,即使在Visual C++ 2017(但不是2013)下,即使你使用system_category
,ERROR_PATH_NOT_FOUND
也可以工作。是的,只有当使用system_category
而不是generic_category
时才能正常工作,微软已经做得非常好了。 - Yongwei Wu
std::system_category()
是一场灾难。请查看**这里**。标准基本上没有为同时具有errno
和本地错误代码的系统提供任何规定。 - rustyxsystem_category
一起使用,并且它将与std::errc
常量相当。如果使用 Visual C++ 2015 或更高版本(在您发表评论时可用),那么std::error_code(ERROR_FILE_NOT_FOUND, std::system_category()) == std::errc::no_such_file_or_directory
和std::error_code(ERROR_PATH_NOT_FOUND, std::system_category()) == std::errc::no_such_file_or_directory
是正确的。 - Yongwei Wu