How do I define a null ostream that inherits publicly std::ostream and
ignores anything that would otherwise be output? 13 6652
Angel Tsankov wrote: How do I define a null ostream that inherits publicly std::ostream and ignores anything that would otherwise be output?
You could create an ofstream that writes to /dev/null
Not portable, but easy :)
Angel Tsankov wrote: How do I define a null ostream that inherits publicly std::ostream and ignores anything that would otherwise be output?
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
try this:
#include <iostream>
struct null_streambuf
: public std::streambuf
{
void overflow(char c)
{
}
};
int main()
{
null_streambuf nullbuf;
// replace buffer of existing stream
std::streambuf * origbuf = std::cout.rdbuf(&nullbuf);
std::cout << "this goes into oblivion";
std::cout.rdbuf(origbuf);
// create null ostream
std::ostream out(&nullbuf);
out << "this goes into oblivion too...";
}
-- peter
to correct myself, Thomas Tutone's version's author hasn't been too
lazy to ignore non-char character traits and the proper return value of
the streambuf::overflow member. use that one. :)
"Thomas Tutone" <Th***********@yahoo.com> wrote in message
news:11**********************@g47g2000cwa.googlegr oups.com... Angel Tsankov wrote:
How do I define a null ostream that inherits publicly std::ostream and ignores anything that would otherwise be output?
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
Thanks, Tom! This seems to be exactly what I need:)
"Thomas Tutone" <Th***********@yahoo.com> wrote in message
news:11**********************@g47g2000cwa.googlegr oups.com... Angel Tsankov wrote:
How do I define a null ostream that inherits publicly std::ostream and ignores anything that would otherwise be output?
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
May I save the call init(&m_sbuf); in basic_onullstream's ctor as this
same call is made in basic_ostream's ctor?
Angel Tsankov wrote: "Thomas Tutone" <Th***********@yahoo.com> wrote in message news:11**********************@g47g2000cwa.googlegr oups.com...
Angel Tsankov wrote:
How do I define a null ostream that inherits publicly std::ostream and ignores anything that would otherwise be output?
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
May I save the call init(&m_sbuf); in basic_onullstream's ctor as this same call is made in basic_ostream's ctor?
My preference would be to keep init and change the initialiser list
basic_onullstream()
{
init(&m_sbuf);
}
john
"John Harrison" <jo*************@hotmail.com> wrote in message
news:yN***************@newsfe1-gui.ntli.net... Angel Tsankov wrote: "Thomas Tutone" <Th***********@yahoo.com> wrote in message news:11**********************@g47g2000cwa.googlegr oups.com...
Angel Tsankov wrote:
How do I define a null ostream that inherits publicly std::ostream and ignores anything that would otherwise be output?
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
May I save the call init(&m_sbuf); in basic_onullstream's ctor as this same call is made in basic_ostream's ctor?
My preference would be to keep init and change the initialiser list
basic_onullstream() { init(&m_sbuf); }
john
Well, I'm not sure this is a good idea, as ctors of virtual bases must
be called by any derived class ctor, right?
Angel Tsankov wrote: How do I define a null ostream that inherits publicly std::ostream and ignores anything that would otherwise be output?
You can just set the failbit on any ostream. This makes it effectively
ignore all output sent to it until you unset the bit.
Angel Tsankov wrote: "John Harrison" <jo*************@hotmail.com> wrote in message news:yN***************@newsfe1-gui.ntli.net...
Angel Tsankov wrote:
"Thomas Tutone" <Th***********@yahoo.com> wrote in message news:11**********************@g47g2000cwa.googlegr oups.com...
Angel Tsankov wrote:
> How do I define a null ostream that inherits publicly std::ostream and > ignores anything that would otherwise be output? 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
May I save the call init(&m_sbuf); in basic_onullstream's ctor as this same call is made in basic_ostream's ctor?
My preference would be to keep init and change the initialiser list
basic_onullstream() { init(&m_sbuf); }
john
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
>>>> May I save the call init(&m_sbuf); in basic_onullstream's ctor as this same call is made in basic_ostream's ctor?
My preference would be to keep init and change the initialiser list
basic_onullstream() { init(&m_sbuf); }
john
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?
> 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
>> 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
What compiler are you using? This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
by: Victor Irzak |
last post by:
Hello,
I have an ABC.
it supports:
ostream & operator <<
I also have a derived class that supports this operator.
How can I call operator << of the base class for derived object??? Is it...
|
by: Stewart Gordon |
last post by:
Using g++ 3.1, Mac OS X.
I'm doing something trying to involve a null output stream, i.e. a
stream that just discards what goes into it.
The idea is that it gives a simple way of producing...
|
by: Bo Peng |
last post by:
Dear list,
I need to add "disable output" feature to a bunch of objects that output
to ostream (or ofstream). The least intrusive way to do this is pass
them ofstream("/dev/null") but this makes...
|
by: nish.parikh |
last post by:
Hi,
I am using std::cout to print a char pointer that is NULL. Subsequent
calls to std::cout dont print anything. Is this the expected behavior?
example:
#include <iostream>
int main( int...
|
by: sandy |
last post by:
I need (okay, I want) to make a dynamic array of my class 'Directory',
within my class Directory (Can you already smell disaster?)
Each Directory can have subdirectories so I thought to put these...
|
by: isanbard |
last post by:
Hi all,
I'd like to do something like this:
#include <ostream>
std::ostream cnull(0);
#ifdef NDEBUG
#define DOUT cnull
|
by: toton |
last post by:
Hi,
I want to have a log stream, where I can put certain logs. The log
can be console or file or any other ui widget.
This part I had already done with. However I want log stream
(std::ostream...
|
by: syang8 |
last post by:
Any one can specify the problem of the following code? The compiling
error is on the friend function. If the base class is not inherited
from ostream, or I just remove the friend function from the...
|
by: coomberjones |
last post by:
I have a few std::strings that I am using to store raw binary data,
each of which may very well include null bytes at any point or
points. I want to slap them together into a single string, so I...
|
by: taylorcarr |
last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
|
by: aa123db |
last post by:
Variable and constants
Use var or let for variables and const fror constants.
Var foo ='bar';
Let foo ='bar';const baz ='bar';
Functions
function $name$ ($parameters$) {
}
...
|
by: emmanuelkatto |
last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud.
Please let me know.
Thanks!
Emmanuel
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
by: Sonnysonu |
last post by:
This is the data of csv file
1 2 3
1 2 3
1 2 3
1 2 3
2 3
2 3
3
the lengths should be different i have to store the data by column-wise with in the specific length.
suppose the i have to...
|
by: Hystou |
last post by:
There are some requirements for setting up RAID:
1. The motherboard and BIOS support RAID configuration.
2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
|
by: marktang |
last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
|
by: Hystou |
last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
|
by: Oralloy |
last post by:
Hello folks,
I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>".
The problem is that using the GNU compilers,...
| |