看起来你应该使用system_category()
for GetLastError()
/errno
并且它会在两个平台上做正确的事情。
如果您已经有errc
,请改用generic_category()
(或make_error_code
)。
这是一些带有“地址已在使用”错误的测试。
#include <iostream>
#include <system_error>
#ifdef _WIN32
#include <WinError.h>
#define LAST_ERROR WSAEADDRINUSE
#else
#include <errno.h>
#define LAST_ERROR EADDRINUSE
#endif
#define ERRC std::errc::address_in_use
#define TRY(...) \
{ \
std::error_code ec = {__VA_ARGS__}; \
std::cout << std::boolalpha << (ec == ERRC) << "\t" << ec.value() << "\t" \
<< ec.message() << "\n"; \
}
int main() {
TRY(static_cast<int>(ERRC), std::system_category())
TRY(static_cast<int>(ERRC), std::generic_category()) // note: same as make_error_code
TRY(static_cast<int>(LAST_ERROR), std::system_category())
TRY(static_cast<int>(LAST_ERROR), std::generic_category()) // note: same as make_error_code
return 0;
}
在 Windows 上:
false 100 Cannot create another system semaphore.
true 100 address in use
true 10048 Only one usage of each socket address (protocol/network address/port) is normally permitted.
false 10048 unknown error
在 POSIX 上:
true 98 Address already in use
true 98 Address already in use
true 98 Address already in use
true 98 Address already in use
使用这些等效错误代码的三元组进行测试时,我得到了类似的结果:
equivalent errc Windows POSIX
errc::broken_pipe ERROR_BROKEN_PIPE EPIPE
errc::filename_too_long ERROR_BUFFER_OVERFLOW ENAMETOOLONG
errc::not_supported ERROR_NOT_SUPPORTED ENOTSUP
errc::operation_would_block WSAEWOULDBLOCK EWOULDBLOCK
如果有人感兴趣,这里有一个std::errc
映射到==
WinError.h
常量的 s 列表。这是检查if (std::error_code(static_cast<int>(win_error_constant), std::system_category()) == errc)
。
address_family_not_supported:
WSAEAFNOSUPPORT
address_in_use:
WSAEADDRINUSE
address_not_available:
WSAEADDRNOTAVAIL
already_connected:
WSAEISCONN
argument_list_too_long:
argument_out_of_domain:
bad_address:
WSAEFAULT
bad_file_descriptor:
WSAEBADF
bad_message:
broken_pipe:
ERROR_BROKEN_PIPE
connection_aborted:
WSAECONNABORTED
connection_already_in_progress:
WSAEALREADY
connection_refused:
WSAECONNREFUSED
connection_reset:
WSAECONNRESET
cross_device_link:
ERROR_NOT_SAME_DEVICE
destination_address_required:
WSAEDESTADDRREQ
device_or_resource_busy:
ERROR_BUSY_DRIVE
ERROR_BUSY
ERROR_OPEN_FILES
ERROR_DEVICE_IN_USE
directory_not_empty:
ERROR_DIR_NOT_EMPTY
executable_format_error:
file_exists:
ERROR_FILE_EXISTS
ERROR_ALREADY_EXISTS
file_too_large:
filename_too_long:
ERROR_BUFFER_OVERFLOW
WSAENAMETOOLONG
function_not_supported:
ERROR_INVALID_FUNCTION
host_unreachable:
WSAEHOSTUNREACH
identifier_removed:
illegal_byte_sequence:
inappropriate_io_control_operation:
interrupted:
WSAEINTR
invalid_argument:
ERROR_INVALID_HANDLE
ERROR_INVALID_PARAMETER
ERROR_NEGATIVE_SEEK
ERROR_DIRECTORY
ERROR_REPARSE_TAG_INVALID
WSAEINVAL
invalid_seek:
io_error:
ERROR_SEEK
ERROR_WRITE_FAULT
ERROR_READ_FAULT
ERROR_OPEN_FAILED
ERROR_CANTOPEN
ERROR_CANTREAD
ERROR_CANTWRITE
is_a_directory:
message_size:
WSAEMSGSIZE
network_down:
WSAENETDOWN
network_reset:
WSAENETRESET
network_unreachable:
WSAENETUNREACH
no_buffer_space:
WSAENOBUFS
no_child_process:
no_link:
no_lock_available:
ERROR_LOCK_VIOLATION
ERROR_LOCKED
no_message_available:
no_message:
no_protocol_option:
WSAENOPROTOOPT
no_space_on_device:
ERROR_HANDLE_DISK_FULL
ERROR_DISK_FULL
no_stream_resources:
no_such_device_or_address:
no_such_device:
ERROR_INVALID_DRIVE
ERROR_BAD_UNIT
ERROR_DEV_NOT_EXIST
no_such_file_or_directory:
ERROR_FILE_NOT_FOUND
ERROR_PATH_NOT_FOUND
ERROR_BAD_NETPATH
ERROR_INVALID_NAME
no_such_process:
not_a_directory:
not_a_socket:
WSAENOTSOCK
not_a_stream:
not_connected:
WSAENOTCONN
not_enough_memory:
ERROR_NOT_ENOUGH_MEMORY
ERROR_OUTOFMEMORY
not_supported:
ERROR_NOT_SUPPORTED
operation_canceled:
ERROR_OPERATION_ABORTED
operation_in_progress:
WSAEINPROGRESS
operation_not_permitted:
operation_not_supported:
WSAEOPNOTSUPP
operation_would_block:
WSAEWOULDBLOCK
owner_dead:
permission_denied:
ERROR_ACCESS_DENIED
ERROR_INVALID_ACCESS
ERROR_CURRENT_DIRECTORY
ERROR_WRITE_PROTECT
ERROR_SHARING_VIOLATION
ERROR_CANNOT_MAKE
ERROR_NOACCESS
WSAEACCES
protocol_error:
protocol_not_supported:
WSAEPROTONOSUPPORT
read_only_file_system:
resource_deadlock_would_occur:
resource_unavailable_try_again:
ERROR_NOT_READY
ERROR_RETRY
result_out_of_range:
state_not_recoverable:
stream_timeout:
text_file_busy:
timed_out:
WSAETIMEDOUT
too_many_files_open_in_system:
too_many_files_open:
ERROR_TOO_MANY_OPEN_FILES
WSAEMFILE
too_many_links:
too_many_symbolic_link_levels:
value_too_large:
wrong_protocol_type:
WSAEPROTOTYPE.0