C%2b%2b Ostream Dev Null
‘Thomas Tutone’ <Th***********@yahoo.com> wrote in message
news:11**********************@g47g2000cwa.googlegr oups.com…
Angel Tsankov wrote:
How do I define a null ostream that inherits publicly std::ostream
and
ignores anything that would otherwise be output?I cribbed this from c.l.c++ years ago — don’t remember the original
author (but it’s not me):
#include <streambuf>
#include <ostream>
template <class cT, class traits = std::char_traits<cT> >
class basic_nullbuf: public std::basic_streambuf<cT, traits> {
typename traits::int_type overflow(typename traits::int_type c)
{
return traits::not_eof(c); // indicate success
}
};
template <class cT, class traits = std::char_traits<cT> >
class basic_onullstream: public std::basic_ostream<cT, traits> {
public:
basic_onullstream():
std::basic_ios<cT, traits>(&m_sbuf),
std::basic_ostream<cT, traits>(&m_sbuf)
{
init(&m_sbuf);
}
private:
basic_nullbuf<cT, traits> m_sbuf;
};
typedef basic_onullstream<char> onullstream;
typedef basic_onullstream<wchar_t> wonullstream;
Best regards,
Tom
Thanks, Tom! This seems to be exactly what I need:)
< cpp | io | basic ostream
C++ Language Standard Library Headers Freestanding and hosted implementations Named requirements Language support library Concepts library(C++20) Diagnostics library Utilities library Strings library Containers library Iterators library Ranges library(C++20) Algorithms library Numerics library Localizations library Input/output library Filesystem library(C++17) Regular expressions library(C++11) Atomic operations library(C++11) Thread support library(C++11) Technical Specifications Input/output library I/O manipulators C-style I/O Buffers
(deprecated in C++98)
(C++20)
Streams Abstractions File I/O String I/O Array I/O
(deprecated in C++98)
(deprecated in C++98)
(deprecated in C++98)
Synchronized Output
(C++20)
Types Error category interface
(C++11)
(C++11)
std::basic_ostream
This benchmark formats a simple message and prints it to the output stream redirected to / dev / null. It uses the Google Benchmark library GOOGLE-BENCH to measure timings: #include #include #include #include ostream.h void printf ( benchmark:: State & s ) while ( s. I combined three different proposals above and one writing directly to /dev/null (so it involves kernel.) Surprisingly the NullStream that got the most votes performed the worst. Here are results for 100,000,000 writes: a) /dev/null: 30 seconds b) NullStream: 50 seconds c) badbit: 16 seconds (the winner in speed, but cannot test for errors!).
Global objects Member functions
(C++11)
Formatted output Unformatted output Positioning Miscellaneous
(C++11)
Member classes Non-member functions
- 这个问题已经在这里有了答案: 9年前关闭。 Possible Duplicate: Implementing a no-op std::ostream 在c中是否有与NULL等价的流?我想编写一个函数,如果用户希望将内部输出输出到某个地方,则将其带入一个流中,但是如果没有,则输出进入某个伪造的地方.
- To define the ostream class in C sources, the ostream header file must be included. To use the predefined ostream objects (std::cerr, std::cout etc.) the header file must be included. 6.4.1.1: Writing to `ostream’ objects The class ostream supports both formatted and binary output.
- An Ostream is an abstract base class for all output systems (streams, files, token lists.
Defined in header <ostream>
(1)
template<class CharT, class Traits>
basic_ostream<CharT,Traits>& operator<<( basic_ostream<CharT,Traits>& os,
CharT ch );
template<class CharT, class Traits>
basic_ostream<CharT,Traits>& operator<<( basic_ostream<CharT,Traits>& os,
char ch );
template<class Traits >
basic_ostream<char,Traits>& operator<<( basic_ostream<char,Traits>& os,
char ch );
template<class Traits >
basic_ostream<char,Traits>& operator<<( basic_ostream<char,Traits>& os,
signedchar ch );
template<class Traits >
basic_ostream<char,Traits>& operator<<( basic_ostream<char,Traits>& os,
unsignedchar ch );
(2)
template<class CharT, class Traits >
basic_ostream<CharT,Traits>& operator<<( basic_ostream<CharT,Traits>& os,
const CharT* s );
template<class CharT, class Traits >
basic_ostream<CharT,Traits>& operator<<( basic_ostream<CharT,Traits>& os,
constchar* s );
template<class Traits >
basic_ostream<char,Traits>& operator<<( basic_ostream<char,Traits>& os,
constchar* s );
template<class Traits >
basic_ostream<char,Traits>& operator<<( basic_ostream<char,Traits>& os,
constsignedchar* s );
template<class Traits >
basic_ostream<char,Traits>& operator<<( basic_ostream<char,Traits>& os,
constunsignedchar* s );
template<class Ostream, class T >
Ostream&& operator<<( Ostream&& os,
const T& value );
(3) (since C++11)
deleted overloads for basic_ostream and UTF character/array
(4)(since C++20)
template<class Traits >
basic_ostream<char,Traits>& operator<<( basic_ostream<char,Traits>& os,
wchar_t ch )= delete;
template<class Traits >
basic_ostream<char,Traits>& operator<<( basic_ostream<char,Traits>& os,
char8_t ch )= delete;
template<class Traits >
basic_ostream<char,Traits>& operator<<( basic_ostream<char,Traits>& os,
char16_t ch )= delete;
template<class Traits >
basic_ostream<char,Traits>& operator<<( basic_ostream<char,Traits>& os,
char32_t ch )= delete;
template<class Traits >
basic_ostream<wchar_t,Traits>& operator<<( basic_ostream<wchar_t,Traits>& os,
char8_t ch )= delete;
template<class Traits >
basic_ostream<wchar_t,Traits>& operator<<( basic_ostream<wchar_t,Traits>& os,
char16_t ch )= delete;
template<class Traits >
basic_ostream<wchar_t,Traits>& operator<<( basic_ostream<wchar_t,Traits>& os,
char32_t ch )= delete;
template<class Traits >
basic_ostream<char,Traits>& operator<<( basic_ostream<char,Traits>& os,
constwchar_t* ch )= delete;
template<class Traits >
basic_ostream<char,Traits>& operator<<( basic_ostream<char,Traits>& os,
const char8_t* ch )= delete;
template<class Traits >
basic_ostream<char,Traits>& operator<<( basic_ostream<char,Traits>& os,
constchar16_t* ch )= delete;
template<class Traits >
basic_ostream<char,Traits>& operator<<( basic_ostream<char,Traits>& os,
constchar32_t* ch )= delete;
template<class Traits >
basic_ostream<wchar_t,Traits>& operator<<( basic_ostream<wchar_t,Traits>& os,
const char8_t* ch )= delete;
template<class Traits >
basic_ostream<wchar_t,Traits>& operator<<( basic_ostream<wchar_t,Traits>& os,
constchar16_t* ch )= delete;
template<class Traits >
basic_ostream<wchar_t,Traits>& operator<<( basic_ostream<wchar_t,Traits>& os,
constchar32_t* ch )= delete;
Inserts a character or a character string.
1) Behaves as an FormattedOutputFunction. After constructing and checking the sentry object, inserts the character ch
. If the type of the character is not CharT
, it is first converted with os.widen(ch). Padding is determined as follows: if os.width()>1
, then os.width()-1
copies of os.fill()
are added to the output character to form the output character sequence.If (out.flags()&std::ios_base::adjustfield)std::ios_base::left, the fill characters are placed after the output character, otherwise before. After insertion, os.width(0) is called to cancel the effects of std::setw, if any.
2)
Behaves as an FormattedOutputFunction. After constructing and checking the sentry object, inserts successive characters from the character array whose first element is pointed to by s
.
- for the first and third overloads (where
CharT
matches the type ofch
), exactlytraits::length(s)
characters are inserted. - for the second overload, exactly std::char_traits<char>::length(s) characters are inserted.
- for the last two overloads, exactly traits::length(reinterpret_cast<constchar*>(s)) are inserted.
Before insertion, first, all characters are widened using os.widen(), then padding is determined as follows: if the number of characters to insert is less than os.width()
, then enough copies of os.fill()
are added to the character sequence to make its length equal os.width()
. If (out.flags()&std::ios_base::adjustfield)std::ios_base::left, the fill characters are added at the end of the output sequence, otherwise they are added before the output sequence.After insertion, width(0) is called to cancel the effects of std::setw, if any.
The behavior is undefined if s
is a null pointer.
3) Calls the appropriate insertion operator, given an rvalue reference to an output stream object (equivalent to os << value). This overload participates in overload resolution only if the expression os << value is well-formed and Ostream
is a class type publicly and unambiguously derived from std::ios_base.
4) Overloads that accept char16_t, char32_t etc (or null terminated sequence thereof) are deleted: std::cout<< u’X’ is not allowed. Previously, these would print an integer or pointer value.
[edit]Parameters
os — output stream to insert data to ch — reference to a character to insert s — pointer to a character string to insert
[edit]Return value
1–2)os
[edit]Notes
Before LWG#1203, code such as (std::ostringstream()<<1.2).str() does not compile.
[edit]Example
Output:
[edit]Defect reports
The following behavior-changing defect reports were applied retroactively to previously published C++ standards.
DR Applied to Behavior as published Correct behaviorLWG 1203 C++11 overload for rvalue stream returned lvalue reference to the base class returns rvalue reference to the derived classLWG 2534 C++11 overload for rvalue stream was not constrained constrained
[edit]See also
C 2b 2b Ostream Dev Null Test
inserts formatted data
(public member function)[edit] widens characters
(public member function of std::basic_ios<CharT,Traits>
)[edit]
C 2b 2b Ostream Dev Null Command
Retrieved from ‘https://en.cppreference.com/mwiki/index.php?title=cpp/io/basic_ostream/operator_ltlt2&oldid=116601'