title: How to get a file descriptor from a std::basic_ios for clang on OS X?
body: <p>I'm porting some code to Darwin OS X and as part of the change, we go from gcc to the clang compiler.</p>
  
  <p>In the code, there is a function dating from 2005 and posted several places on the Internet.  It provides functionality for several different old versions of GCC, I've edited out all but the last it offers, v3.4.0 or later.  The code depends upon two GCC specific classes: <code>__gnu_cxx::stdio_filebuf</code> and <code>__gnu_cxx::stdio_sync_filebuf</code>.</p>
  
  <pre><code>//! Similar to fileno(3), but taking a C++ stream as argument instead of a
  //! FILE*.  Note that there is no way for the library to track what you do with
  //! the descriptor, so be careful.
  //! \return  The integer file descriptor associated with the stream, or -1 if
  //!   that stream is invalid.  In the latter case, for the sake of keeping the
  //!   code as similar to fileno(3), errno is set to EBADF.
  //! \see  The &lt;A HREF="http://www.ginac.de/~kreckel/fileno/"&gt;upstream page at
  //!   http://www.ginac.de/~kreckel/fileno/&lt;/A&gt; of this code provides more
  //!   detailed information.
  template &lt;typename charT, typename traits&gt;
  inline int
  fileno_hack(const std::basic_ios&lt;charT, traits&gt;&amp; stream)
  {
    // Some C++ runtime libraries shipped with ancient GCC, Sun Pro,
    // Sun WS/Forte 5/6, Compaq C++ supported non-standard file descriptor
    // access basic_filebuf&lt;&gt;::fd().  Alas, starting from GCC 3.1, the GNU C++
    // runtime removes all non-standard std::filebuf methods and provides an
    // extension template class __gnu_cxx::stdio_filebuf on all systems where
    // that appears to make sense (i.e. at least all Unix systems).  Starting
    // from GCC 3.4, there is an __gnu_cxx::stdio_sync_filebuf, in addition.
    // Sorry, darling, I must get brutal to fetch the darn file descriptor!
    // Please complain to your compiler/libstdc++ vendor...
  #if defined(__GLIBCXX__) || defined(__GLIBCPP__)
    // OK, stop reading here, because it's getting obscene.  Cross fingers!
  # if defined(__GLIBCXX__)  // &gt;= GCC 3.4.0
    // This applies to cin, cout and cerr when not synced with stdio:
    typedef __gnu_cxx::stdio_filebuf&lt;charT, traits&gt; unix_filebuf_t;
    unix_filebuf_t* fbuf = dynamic_cast&lt;unix_filebuf_t*&gt;(stream.rdbuf());
    if (fbuf != NULL) {
      return fbuf-&gt;fd();
    }
  
    // This applies to filestreams:
    typedef std::basic_filebuf&lt;charT, traits&gt; filebuf_t;
    filebuf_t* bbuf = dynamic_cast&lt;filebuf_t*&gt;(stream.rdbuf());
    if (bbuf != NULL) {
      // This subclass is only there for accessing the FILE*.  Ouuwww, sucks!
      struct my_filebuf : public std::basic_filebuf&lt;charT, traits&gt; {
        int fd() { return this-&gt;_M_file.fd(); }
      };
      return static_cast&lt;my_filebuf*&gt;(bbuf)-&gt;fd();
    }
  
    // This applies to cin, cout and cerr when synced with stdio:
    typedef __gnu_cxx::stdio_sync_filebuf&lt;charT, traits&gt; sync_filebuf_t;
    sync_filebuf_t* sbuf = dynamic_cast&lt;sync_filebuf_t*&gt;(stream.rdbuf());
    if (sbuf != NULL) {
      return fileno(sbuf-&gt;file());
    }
  # endif
  #else
  #  error "Does anybody know how to fetch the bloody file descriptor?"
    return stream.rdbuf()-&gt;fd();  // Maybe a good start?
  #endif
  
    errno = EBADF;
    return -1;
  }
  </code></pre>
  
  <p>The question is, for clang 5.1 on OS X Mavericks, what would be the way to computed the file descriptor for a <code>std::basic_ios</code>?</p>