"Null" I/O Stream?

I

isanbard

Hi all,

I'd like to do something like this:

#include <ostream>

std::eek:stream cnull(0);

#ifdef NDEBUG
#define DOUT cnull
#else
#define DOUT std::cerr
#endif

int main()
{
// ...
DOUT << "This is a debug message\n";
// ...
}

My question is, is the above okay? That is, is passing 0 in for the
stream buffer okay and portable and won't make things crash?

As an added bonus, I'd like to have all cnull symbols and the debug
string removed from the resulting executable when NDEBUG is defined.
Will this happen with the above?

Or is there a better way?

Thanks!
-bw
 
E

eriwik

Hi all,

I'd like to do something like this:

#include <ostream>

std::eek:stream cnull(0);

#ifdef NDEBUG
#define DOUT cnull
#else
#define DOUT std::cerr
#endif

int main()
{
// ...
DOUT << "This is a debug message\n";
// ...

}

My question is, is the above okay? That is, is passing 0 in for the
stream buffer okay and portable and won't make things crash?

What I can tell ostream does not have any constructor taking 0 as an
argument, what were you hoping to accomplish?

As an added bonus, I'd like to have all cnull symbols and the debug
string removed from the resulting executable when NDEBUG is defined.
Will this happen with the above?

Perhaps you want something like this (I'm not good at macros and it's
untested, but it should give you an idea):

#ifndef NDEBUG
#define DEBUG(x) (std::cerr << x;)
#else
#define DEBUG(x) // not sure about this line, might need () or
something
#endif

And then use it like:
DEBUG("Debug-message")
or
DEBUG(foo())
 
M

Markus Moll

Hi

What I can tell ostream does not have any constructor taking 0 as an
argument, what were you hoping to accomplish?

How about
"public: explicit basic_ostream(basic_streambuf<char_type, traits>* sb)"?

Anyway, while you can construct this stream, you cannot use it. Outputting
to a stream invokes something equivalent to rdbuf()->sputc(...). Undefined
behavior by dereferencing a null pointer.

I don't think that this is specified. I might be wrong.
Perhaps you want something like this (I'm not good at macros and it's
untested, but it should give you an idea):

#ifndef NDEBUG
#define DEBUG(x) (std::cerr << x;)
#else
#define DEBUG(x) // not sure about this line, might need () or
something
#endif

Then DEBUG(1 << 4) outputs "1" and then "4"?
People might expect 16.
Another suggestion:

#ifndef NDEBUG
std::eek:stream& dout = std::cerr;
#else
struct dummy_ostream {
template<typename T> dummy_ostream& operator<< (T) { return *this; }
} dout;
#endif

If you need other members, add them...

Markus
 
M

Michael DOUBEZ

(e-mail address removed) a écrit :
Hi all,

I'd like to do something like this:

#include <ostream>

std::eek:stream cnull(0);

#ifdef NDEBUG
#define DOUT cnull
#else
#define DOUT std::cerr
#endif

int main()
{
// ...
DOUT << "This is a debug message\n";
// ...
}

My question is, is the above okay? That is, is passing 0 in for the
stream buffer okay and portable and won't make things crash?

As an added bonus, I'd like to have all cnull symbols and the debug
string removed from the resulting executable when NDEBUG is defined.
Will this happen with the above?

Or is there a better way?

Thanks!
-bw

Make the code unreachable:
#define DOUT while(0) std::cout

And turn off warning concerning unreachable code

Beware of bug like:
DOUT<<"Result: "<<myImportantFunction()<<std::endl;
because myImportantFunction() won't be executed either (same as assert).

Michael
 
R

Ron Natalie

Hi all,

I'd like to do something like this:

#include <ostream>

std::eek:stream cnull(0);
You need a null streambuf. It's easy to write.
Just define the overflow() function to do nothing (throws
the characters away) and use that to initialize your output
stream.
 

Ask a Question

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.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,780
Messages
2,569,608
Members
45,250
Latest member
Charlesreero

Latest Threads

Top