The conventional method
template<class charT,
class traits = std::char_traits<charT> >
class logostream :
public std::basic_ostream<charT, traits>
{
private:
logoutbuf<charT, traits> buf_;
public:
logostream() :
std::basic_ostream<charT, traits>(&buf_), buf_() { }
};
What's wrong with this?
A common solution is to create a subclass of std::basic_ostream with the stream buffer as a member which can be passed to the std::basic_ostream constructor.
Having the stream buffer as a member introduces a potential initialisation problem. The solution to the problem introduces a further problem hidden deep within the C++ standard. However, this second problem is also easily fixed.
If the stream buffer is dereferenced in std::basic_ostream's constructor or in its destructor, undefined behaviour can occur as the stream buffer will not have been initialised. At least one well known and widely used standard library implementation does nothing to avoid this and does not need to. Library implementers know their stream implementations and whether or not protection is needed. We, as stream extenders writing for potentially any number of different stream implementations, do not. There is no guarantee in the C++ standard to fall back on either.