问题
Is there a way to create an ostream instance which basically doesn't do anything ?
For example :
std::ostream dummyStream(...);
dummyStream << "Nothing will be printed";
I could just create an ostringstream, but data will be buffered (and I really don't want to make anything with them, so it adds a useless overhead).
Any idea ?
[edit] Found this related question which suits my needs. However, I think it could be useful to have a answer saying how to create a valid (no badbit) output stream with standard c++.
回答1:
You need a custom streambuf.
class NullBuffer : public std::streambuf
{
public:
int overflow(int c) { return c; }
};
You can then use this buffer in any ostream class
NullBuffer null_buffer;
std::ostream null_stream(&null_buffer);
null_stream << "Nothing will be printed";
streambuf::overflow
is the function called when the buffer has to output data to the actual destination of the stream. The NullBuffer
class above does nothing when overflow is called so any stream using it will not produce any output.
回答2:
If this is to disable logging output, your dummyStream
would still cause arguments to be evaluated. If you want to minimize impact when logging is disabled, you can rely on a conditional, such as:
#define debugStream \
if (debug_disabled) {} \
else std::cerr
So if you have code like:
debugStream << "debugging output: " << foo() << std::endl;
No arguments will be evaluated if debug_disabled
is true.
回答3:
The basic method voor new stream classes is:
- Derive a class from
std::streambuf
; - Override the virtual functions in that class. This is where the real work is done. In your case, empty implementations should be good enough.
- Derive a class from
std::ostream
with one member, your streambuf class. - The constructor of your streamclass should forward the pointer to that member to the base constructor of std::ostream.
I'm afraid you won't get rid of the formatting step, though.
Hopefully this gives you some pointers; I don't have the time to expand this into a full answer, sorry.
Update: See john's answer for details.
回答4:
If you are concerned about the overhead of your debugger then you can write a very simple code to void out your debug messages on compilation. This is what I use for my c++ programs.
#include <iostream>
#define DEBUGGING // Define this in your config.h or not.
#ifdef DEBUGGING
/*
* replace std::cout with your stream , you don't need to
* worry about the context since macros are simply search
* and replace on compilation.
*/
#define LOG_START std::cout <<
#define LOG_REDIR <<
#define LOG_END << std::endl;
#else
#define LOG_START (void)
#define LOG_REDIR ;(void)
#define LOG_END ;
#endif // DEBUGGING
int main(){
LOG_START "This is a log message " LOG_REDIR "Still a log message." LOG_END;
return 0;
}
Now when making your project , check if the user wants to disable the logging , if so , just undefine the DEBUGGING macro or whatever macro you choose to check for.
Now your code will be optimized by the compiler , Because when anything is voided , it will not be included in the resulting binary(most of the time) , making the binary production ready.
回答5:
I needed a null stream that was of type ostream so I did something like this:
struct NullStream: public stringstream {
NullStream(): stringstream() {}
};
template<typename T>
void operator<<(const NullStream&, const T&) {}
Application code:
NullStream ns;
ostream &os = ns;
os << "foo";
The real issue is all the public methods that I inherited but don't care about so I just didn't bother overriding them.
来源:https://stackoverflow.com/questions/11826554/standard-no-op-output-stream