473,776 Members | 1,568 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

"Null" I/O Stream?

Hi all,

I'd like to do something like this:

#include <ostream>

std::ostream 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

Dec 12 '06 #1
4 2208
On Dec 13, 1:04 am, isanb...@gmail. com wrote:
Hi all,

I'd like to do something like this:

#include <ostream>

std::ostream 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())

--
Erik Wikström

Dec 13 '06 #2
Hi

er****@student. chalmers.se wrote:
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(b asic_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.
>On Dec 13, 1:04 am, isanb...@gmail. com wrote:
>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?
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::ostream& dout = std::cerr;
#else
struct dummy_ostream {
template<typena me Tdummy_ostream& operator<< (T) { return *this; }
} dout;
#endif

If you need other members, add them...

Markus

Dec 13 '06 #3
is******@gmail. com a écrit :
Hi all,

I'd like to do something like this:

#include <ostream>

std::ostream 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: "<<myImportantF unction()<<std: :endl;
because myImportantFunc tion() won't be executed either (same as assert).

Michael
Dec 13 '06 #4
is******@gmail. com wrote:
Hi all,

I'd like to do something like this:

#include <ostream>

std::ostream 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.
Dec 13 '06 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

7
20375
by: Pablo J Royo | last post by:
Hello: i have a function that reads a file as an argument and returns a reference to an object that contains some information obtained from the file: FData &ReadFile(string FilePath); But , for example, when the file doesnt exists, i should not return any reference to a bad constructed object, so i need something as a NULL reference object. I suppose i could return a pointer instead, but i have some code written with references which...
13
3088
by: Don Vaillancourt | last post by:
What's going on with Javascript. At the beginning there was the "undefined" value which represented an object which really didn't exist then came the null keyword. But yesterday I stumbled across "null" string. I know that I will get an "undefined" when I try to retrieve something from the DOM which doesn't exist. I have used null myself to initialize or reset variables. But in which
1
3180
by: putty | last post by:
I found a few posts of people asking about insertCell()/insertRow() not working in IE6 SP2, and a few others about getting "null is null or not an object" errors, but no one posted a solution anywhere or noted this tiny yet curious change in SP2.. so here it is: In IE6 SP2 rows must be inserted into a tBody instead of a Table directly, after that you may insert TDs into TRs any way you wish. insertRow() and insertCell() work the same as...
13
3115
by: gary | last post by:
Hi, We all know the below codes are dangerous: { int *p = new int; delete p; delete p; } And we also know the compilers do not delete p if p==NULL. So why compilers do not "p = NULL" automatically after programs do "delete p"?
2
1459
by: Rudolf Ball | last post by:
Hi NG, when trying to drag a UserControl from the toolbox on a Form, this exception rises: The exception was ""null" is not a valid value for the 'stream'.". ? Thank you
14
3169
by: MuZZy | last post by:
Hi, Lately i've been (and still am) fixing some memory leaks problems in the project i just took over when i got this new job. Among the other issues i've noticed that for localy created objects it makes difference to explicitly put them to null after working with them is done - it somehow makes the GC collect them sooner, like here: void SomeFunc() { MyClass c = new MyClass();
1
1257
by: mulham.haffar | last post by:
Hi, im writing an application that uses tcpclient to connect. I have a class which makes the whole network stuff.. As below: Public Class Connections Private Client as tcpclient Private Stream as network stream Public Sub Connect() Client.Connect(..)
5
2283
RMWChaos
by: RMWChaos | last post by:
I am working on a script to create and remove DOM elements, and I want to make it as efficient as possible (no redundancies). Because DOM elements each have their own set of attributes, the function variable list is quite long, and each type of element may not use some of the variables. As a placeholder, I use "null" when there is no value, but this can result in lots of "null" values. So I want to create a function to autofill the "null"...
2
3104
by: crabsdf | last post by:
My project is a single method class which takes a xml object and, using Apache FOP, transforms it into a PDF which is returned as an output stream. Here is full code package transactionStatement; import javax.xml.transform.TransformerFactory; import javax.servlet.*; import org.jdom.*; import java.io.ByteArrayOutputStream; import java.io.File;
0
9628
marktang
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9464
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10289
Oralloy
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10120
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
9923
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8952
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
6722
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
2
3622
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2860
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.