A
Angel Tsankov
How do I define a null ostream that inherits publicly std:stream and
ignores anything that would otherwise be output?
ignores anything that would otherwise be output?
Angel said:How do I define a null ostream that inherits publicly std:stream and
ignores anything that would otherwise be output?
Angel said:How do I define a null ostream that inherits publicly std:stream and
ignores anything that would otherwise be output?
Thomas Tutone said:I cribbed this from c.l.c++ years ago - don't remember the original
author (but it's not me):
#include <streambuf>
#include <ostream>
template <class cT, class traits = std::char_traits<cT> >
class basic_nullbuf: public std::basic_streambuf<cT, traits> {
typename traits::int_type overflow(typename traits::int_type c)
{
return traits::not_eof(c); // indicate success
}
};
template <class cT, class traits = std::char_traits<cT> >
class basic_onullstream: public std::basic_ostream<cT, traits> {
public:
basic_onullstream():
std::basic_ios<cT, traits>(&m_sbuf),
std::basic_ostream<cT, traits>(&m_sbuf)
{
init(&m_sbuf);
}
private:
basic_nullbuf<cT, traits> m_sbuf;
};
typedef basic_onullstream<char> onullstream;
typedef basic_onullstream<wchar_t> wonullstream;
Best regards,
Tom
Thomas Tutone said:I cribbed this from c.l.c++ years ago - don't remember the original
author (but it's not me):
#include <streambuf>
#include <ostream>
template <class cT, class traits = std::char_traits<cT> >
class basic_nullbuf: public std::basic_streambuf<cT, traits> {
typename traits::int_type overflow(typename traits::int_type c)
{
return traits::not_eof(c); // indicate success
}
};
template <class cT, class traits = std::char_traits<cT> >
class basic_onullstream: public std::basic_ostream<cT, traits> {
public:
basic_onullstream():
std::basic_ios<cT, traits>(&m_sbuf),
std::basic_ostream<cT, traits>(&m_sbuf)
{
init(&m_sbuf);
}
private:
basic_nullbuf<cT, traits> m_sbuf;
};
typedef basic_onullstream<char> onullstream;
typedef basic_onullstream<wchar_t> wonullstream;
Best regards,
Tom
Angel said:May I save the call init(&m_sbuf); in basic_onullstream's ctor as this
same call is made in basic_ostream's ctor?
John Harrison said:My preference would be to keep init and change the initialiser list
basic_onullstream()
{
init(&m_sbuf);
}
john
Angel said:How do I define a null ostream that inherits publicly std:stream and
ignores anything that would otherwise be output?
Angel said:Well, I'm not sure this is a good idea, as ctors of virtual bases must
be called by any derived class ctor, right?
Right, but the code above is exactly equivalent to this
basic_onullstream() : std::basic_ios<cT, traits>()
{
init(&m_sbuf);
}
in other words the default constructor of the virtual base class
will be called automatically if you don't explicitly specify a
different constructor.
john
Yes, I totally agree with you on this point. Anyway, could tou tell us
the reasons you prefer to change the initializer list but keep init?
Nothing deep, it's just when I write code like this
template <class cT, class traits = std::char_traits<cT> >
class basic_onullstream: public std::basic_ostream<cT, traits> {
public:
basic_onullstream():
std::basic_ios<cT, traits>(&m_sbuf),
std::basic_ostream<cT, traits>(&m_sbuf)
{
}
my compiler warns me about using pointers to as yet unconstructed
objects. If the basic_ios or basic_ostream constructors were to
dereference the pointer it would be undefined behaviour. They don't
of course but my compiler doesn't know that so I get the warning.
john
Want to reply to this thread or ask your own question?
You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.