Post by Rui MacielIs there any standard output object like cout or cerr that acts like
/dev/null?
There is no such default stream type. You can, however, get that
effect by defining your own null-streambuf that just discards any
characters it is handed over:
template<typename Ch, typename Traits = std::char_traits<Ch> >
struct basic_nullbuf : std::basic_streambuf<Ch, Traits> {
typedef std::basic_streambuf<Ch, Traits> base_type;
typedef typename base_type::int_type int_type;
typedef typename base_type::traits_type traits_type;
virtual int_type overflow(int_type c) {
return traits_type::not_eof(c);
}
};
// convenient typedefs
typedef basic_nullbuf<char> nullbuf;
typedef basic_nullbuf<wchar_t> wnullbuf;
// buffers and streams
nullbuf cnull_obj;
wnullbuf wcnull_obj;
std::ostream cnull(&cnull_obj);
std::wostream wcnull(&wcnull_obj);
Keep the buffers and streams in the same file, to avoid static
initialization order problems. I have seen someone used a not-opened
ofstream to do the same and said that writing to it is a noop, but i
haven't been able to verify that since then. So i would better be on
the safe side writing my own like that. Have fun!
--
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]