473,320 Members | 2,054 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

drop in replacement for stringstream that compiles to nothing

63
hi

I find myself using stringstream all over my code just to build debug messages that I may or may not write to cout later. that is very comfortable while developing but is generally useless for productive code.

so I would like to have some construct that the compiler will remove in the case of NDEBUG. but I dont want to write #ifndef NDEBUG everywhere.

I have tried with this, but seemingly the compiler still cant completely remove NullStream because if I raise the loopcount to something like 10e7 it takes 3 seconds (with MSVS 2005 /O2, even longer with cygwin/gcc344 or ubuntu7.1/gcc413 -O3)

Expand|Select|Wrap|Line Numbers
  1. #include <iostream>
  2. #include <string>
  3. #include <sstream>
  4. using namespace std;
  5.  
  6.  
  7. class NullStream
  8. {
  9. public:
  10.     inline const NullStream& operator<< ( const string & s ) const
  11.     {
  12.         return *this;
  13.     }
  14.     inline const NullStream& operator<< ( int& i ) const
  15.     {
  16.         return *this;
  17.     }
  18.     inline const NullStream& operator<< ( unsigned int& i ) const
  19.     {
  20.         return *this;
  21.     }
  22. };
  23. inline ostream& operator<<( ostream& o, const NullStream& n)
  24. {
  25.     return o;
  26. }
  27. inline ostream& operator<<( ostream& o, const stringstream& s )
  28. {
  29.     o << s.str();
  30.     return o;
  31. }
  32. #ifdef NDEBUG
  33.     typedef NullStream LogStream;
  34. #else
  35.     typedef ostringstream LogStream;
  36. #endif
  37.  
  38.  
  39.  
  40.  
  41. void main_logstream()
  42. {
  43.     cout << __FUNCTION__ << "\n";
  44.     LogStream buf;
  45.  
  46.     double clock_begin = clock();
  47.  
  48.     for ( unsigned int i = 0 ; i < 10e7 ; i++ )
  49.     {
  50.         buf << i << " ";
  51.     }
  52.     cout << "buffer: " << buf << "\n";
  53.     cout << "time: " << ( ( clock() - clock_begin ) / CLOCKS_PER_SEC ) << " s\n";
  54. }
  55.  

I had expected that the compiler should be able to see that the body of that loop only consistst of a couple of calls to functions with empty bodies and thus the whole loop can be removed.

Anybody any suggestions?
Jul 7 '08 #1
2 1563
jabbah
63
uups, seemingly the compiler wont even remove

Expand|Select|Wrap|Line Numbers
  1. for ( unsigned int i = 0 ; i < 10e7 ; i++ )
  2. {
  3.     (void)0;
  4. }
  5.  
am I asking for too much?

CORRECTION: Sorry, that was wrong, for(...){ (void)0; } does get removed!
Jul 7 '08 #2
jabbah
63
yet another correction: seemingly the (void)0; is removed but the (then empty) loop is not ... anyways ...

back to my orignial question - just in case anybody cares - ive not managed to get something that can be #define'd to (void)0 that can be used like a stream but now I have a bunch of macros like

Expand|Select|Wrap|Line Numbers
  1. #define logstream2( stream, m1, m2 ) \
  2.     if (true) { \
  3.         ostream& log_stream = stream; \
  4.         log_stream  << m1 << m2; \
  5.     } else (void)0
  6.  
(and also logstream1, logstream3, etc)

This hasnt got the same interface as stream, ie operator<<(), but its the best I was able to achieve.
Jul 22 '08 #3

Sign in to post your reply or Sign up for a free account.

Similar topics

5
by: Paul Miller | last post by:
We've run into minidom's inabilty to handle large (20+MB) XML files, and need a replacement that can handle it. Unfortunately, we're pretty dependent on a DOM, so a pulldom or SAX replacement is...
2
by: Marc Schellens | last post by:
class myClass { stringstream ioss; istream* is; void f() { .... string initStr("");
19
by: Kai-Uwe Bux | last post by:
Hi folks, I have trouble writing a class, derving from stringstream, that collects item and once it's done will write them to std::cout in one go. It works fine except when I use it as a...
3
by: mlm | last post by:
Hi, I believe that it should not be legal to create a vector (and array) of stringstream since the copy constructor and assignment operators are declared private. However, I don't understand...
10
by: vigacmoe | last post by:
I was trying to cast some strings to integers with stringstream, then this strange problem poped up. Here is my test code: stringstream conv; string from; int to; from = "1"; conv << from;
2
by: Evil Son | last post by:
Hello group, If I switched from mod_php to fast-cgi, would I need to make any changes to my php source? Also, will something like APC still be useful? Will my database connections suddenly...
2
by: ademirzanetti | last post by:
Does anyone know how to debug objects of the type stream in general using GDB ? I am currently using GDB version 6.4. There is no much usefull information about it througout the internet. I am...
16
by: John | last post by:
I am looking for VBA code that will work with Access 2003 to enable dragging and dropping a file/folder name from Windows XP Explorer into an Access form's text box. This is a common functionality...
3
by: jcassan | last post by:
Hello folks. I am new to these forums and have something, which has been stumping me for little while. I am using pspell to spellcheck a scrolling textbox (textarea) containing user input. I...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

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.