Construct std::error_code from errno on POSIX and GetLastError() on Windows

后端 未结 2 1436
猫巷女王i
猫巷女王i 2021-02-05 04:35

My question: What is the correct way to construct std::error_code instances from errno values on POSIX and GetLastError() on Windows so th

2条回答
  •  -上瘾入骨i
    2021-02-05 05:04

    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 value posv, the function shall return error_condition(posv, generic_category()). Otherwise, the function shall return error_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()).

提交回复
热议问题