Implementing binary output to a file in C++11 I would like to have a class which supports outputting bits into a file. This is what I came up with but I fear there are many poor decisions. Should I inherit from a std::ofstream instead of keeping it as a data member?

class OFBitStream final
{
    static const std::size_t bitset_size = 8 * sizeof(unsigned long);
    std::bitset&lt;bitset_size&gt; bitset;
    std::size_t current_bit;

    std::ofstream &amp;_stream;

    void flush() {
        for (auto i = current_bit; i &lt; bitset_size; ++i) {
            bitset[i] = false;
        }

        auto buffer = bitset.to_ulong();
        while (buffer != 0) {
            _stream.write(reinterpret_cast&lt;char *&gt;(&amp;buffer), 1);
            buffer &gt;&gt;= 8;
        }
    }
 public:
    OFBitStream(std::ofstream &amp;stream)
        : bitset(0), current_bit(0), _stream(stream) {}

    ~OFBitStream() {
        flush();
        _stream.close();
    }

    friend OFBitStream &amp;operator&lt;&lt;(OFBitStream &amp;dst, const bool &amp;data)
    {
        dst.bitset[dst.current_bit++] = data;

        if (dst.current_bit == dst.bitset_size) {
            dst.flush();
            dst.current_bit = 0;
        }

        return dst;
    }
};
I would not write an adapter for a stream.
But rather a stream manipulator.

Try this:

#include &lt;ostream&gt;

// The object that does all the work.
class BBoolWrapper
{
    std::ostream&amp;          str;
    mutable std::size_t    position;
    mutable std::bitset&lt;sizeof(unsigned long) * CHAR_BIT&gt; data;

    public:
        BBoolWrapper(std::ostream&amp; str)
            : str(str)
            , position(0)
        {}
        ~BBoolWrapper()
        {
            // If there is data to be flushed when we destroy the
            // object then make sure it goes on the stream.
            flush();
        }

        // Testing state.
        bool empty() const      { return position == 0;}
        bool full() const       { return position == data.size();}

        // Just the way I do it.
        // You can add bits any way you like one at a time.
        void addBit(bool nextbit) const
        {
            data &lt;&lt;= 1;
            data[0] = nextbit;
            ++position;
            if (full())
            {
                flush();
            }
        }

        // Simple Flush to the stream (if there is any data).
        void flush() const
        {
            if (!empty())
            {
                unsigned long value = data.to_ulong();
                str.write(reinterpret_cast&lt;char*&gt;(&amp;value), sizeof(unsigned long));
                position  = 0;
            }
        }

    // The interesting part.
    // If we have an object like this and a bool using the `&lt;&lt;` operator
    // Then add the bit and return this object so we are chaining the
    // `&lt;&lt;` operations.
    friend BBoolWrapper const&amp; operator&lt;&lt;(BBoolWrapper const&amp; stream, bool nextbit)
    {
        stream.addBit(nextbit);
        return stream;
    }

    // If we use any other type with `&lt;&lt;` operator then flush this object.
    // Return the underlying stream object to get back to normal stream
    // operations.
    template&lt;typename T&gt;
    friend std::ostream&amp; operator&lt;&lt;(BBoolWrapper const&amp; stream, T const&amp; val)
    {
        stream.flush();
        return stream.str &lt;&lt; val;
    }
};

// A simple empty class to start the streaming of bits
// via the above wrapper class.
struct BBool
{
    friend BBoolWrapper operator&lt;&lt;(std::ostream&amp; stream, BBool const&amp;)
    {
        return BBoolWrapper(stream);
    }
};


Example:

#include &lt;iostream&gt;
#include &lt;ifstream&gt;

int main()
{

    // Use the BBool() object on a stream to turn binary mode on.
    // It stays on until you add a non boolean object to the chain. (like "\n")
    std::ofstream log("log");
    log       &lt;&lt; BBool() &lt;&lt; false &lt;&lt; true &lt;&lt; false &lt;&lt; false &lt;&lt; false &lt;&lt; false &lt;&lt; false &lt;&lt; true &lt;&lt; "\n";
    std::cout &lt;&lt; BBool() &lt;&lt; false &lt;&lt; true &lt;&lt; false &lt;&lt; false &lt;&lt; false &lt;&lt; false &lt;&lt; false &lt;&lt; true &lt;&lt; "\n";
}