title: Construct std::error_code from errno on POSIX and GetLastError() on Windows
body: <p>My question: What is the correct way to construct <code>std::error_code</code> instances from <code>errno</code> values on POSIX and <code>GetLastError()</code> on Windows so that the instances can be compared to the well-known values from <code>std::errc</code>?</p>
  
  <p>The longer explanation: My goal is to add an <code>std::error_code</code> instance to a self-made exception object that works on POSIX and Windows systems in a C++11ish way.</p>
  
  <p>In my cross-platform application I'm using a self-made I/O class hierarchy that uses the POSIX <code>fopen()</code> and Windows' <code>CreateFile()</code> calls for opening/creating files. If that fails a generic, self-made <code>open_error</code> exception is thrown (it is derived from <code>std::exception</code>, yes, but it's not one of C++'s predefined exception classes). I'm trying to extend this rather bare-bones exception with an error code; to be more precise with C++11's <code>std::error_code</code> if I understood correctly.</p>
  
  <p>My problem is how to construct such an object from <code>errno</code> (in the POSIX case) or <code>GetLastError()</code> (in the Windows case). For POSIX, as far as I've understood things, I can simply use <code>errno</code> in <code>std::error_code</code>'s constructor, e.g. like this:</p>
  
  <pre><code>std::error_code ec(errno, std::generic_category());
  </code></pre>
  
  <p>And that <code>ec</code> should be comparable to the well-known values from <code>std::errc</code>.</p>
  
  <p>For Windows a similar call can be made, of course:</p>
  
  <pre><code>std::error_code ec(::GetLastError(), std::generic_category());
  </code></pre>
  
  <p>But I'm not sure whether or not the values returned by <code>GetLastError()</code> map nicely to the well-known constants from <code>std::errc</code>. I've read <a href="http://www.boost.org/doc/libs/1_52_0/boost/system/windows_error.hpp">in Boost's system library</a> that they do <em>for Boost's implementation of <code>error_code</code></em>, but I'm asking about the <code>std</code> implementation, not about Boost's.</p>
  
  <p>Please don't advice to switch to using C++ streams for file access. I'd love to, but refactoring half of my code is not something I'd like to do right at this very moment.</p>