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

后端 未结 2 1440
猫巷女王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条回答
  •  名媛妹妹
    2021-02-05 04:54

    This is an old question, but I haven't really found a good answer on SO. The accepted answer confuses me a little as it seems to give an error_condition rather than an error_code. I have settled on the following for myself on POSIX:

    std::error_code error_code_from_errno(int errno_code) {
      return std::make_error_code(static_cast(errno_code));
    }
    

    This always gives me the correct category (generic or system). I've had problems in the past where error codes with the same errno code compared as not equal because one had generic_category and the other had system_category.

提交回复
热议问题