My question: What is the correct way to construct std::error_code
instances from errno
values on POSIX and GetLastError()
on Windows so th
That's a quality of implementation issue. The const static object returned by std::system_category()
is relied upon to perform the mapping from the platform-native error code enumeration to the standard std::error_condition
enumeration. Under 17.6.5.14 Value of error codes [value.error.codes]:
Implementations for operating systems that are not based on POSIX are encouraged to define values identical to the operating system’s values.
You can see in http://www.boost.org/doc/libs/1_46_1/libs/system/src/error_code.cpp how Boost performs the mapping; any standard library supplied by your compiler vendor for use on Windows should do something similar.
The intended behaviour is covered in 19.5.1.5p4, describing system_category().default_error_condition(int ev)
:
If the argument
ev
corresponds to a POSIX errno valueposv
, the function shall returnerror_condition(posv, generic_category())
. Otherwise, the function shall returnerror_condition(ev, system_category())
.
So, for example, error_code(ERROR_FILE_NOT_FOUND, std::system_category()).default_error_condition()
will invoke std::system_category().default_error_condition(ERROR_FILE_NOT_FOUND)
, which should return std::error_condition(std::no_such_file_or_directory, std::generic_category())
.