C%2b%2b Ostream Dev 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.

这个问题已经在这里有了答案:
9年前关闭。

  • Returns a quiet NaN (Not-A-Number) value of type double. The NaN values are used to identify undefined or non-representable values for floating-point elements, such as the square root of negative numbers or the result of 0/0.
  • You can then use this buffer in any ostream class. NullBuffer nullbuffer; std::ostream nullstream(&nullbuffer); nullstream.

Possible Duplicate:
Implementing a no-op std::ostream


在c++中是否有与NULL等价的流?我想编写一个函数,如果用户希望将内部输出输出到某个地方,则将其带入一个流中,但是如果没有,则输出进入某个伪造的地方Null
我希望能够选择执行data()data(std::cout)

编辑:取自@Johannes Schaub-litb的邮件here,稍作修改:
使用这些:
现在,这看起来很酷,但是下面的代码更短并且更有效,因为如果将空指针提供给ostream的构造函数,它将自动设置Badbit并静默忽略任何写操作:
该标准从27.6.2.2 [lib.ostream.cons] p1开始,保证了它的工作,ostream描述了streambuf的构造函数,该构造函数采用指向basic_ios的指针:

Effects: Constructs an object of class basic_ostream, assigning initial values to the base class by calling basic_ios<charT,traits>::init(sb).


来自27.4.4.1 [lib.basic.ios.cons] p3badbit的相关功能:

void init(basic_streambuf<charT,traits>* sb);
Postconditions: The postconditions of this function are indicated in Table 89:


表89中的重要行:

rdstate() -- goodbit if sb is not a null pointer, otherwise badbit.


如果设置了27.6.2.6 [lib.ostream.unformatted],将在sentry下进行描述:

Each unformatted output function begins execution by constructing an object of class sentry. If this object returns true, while converting to a value of type bool, the function endeavors to generate the requested output.


这意味着,如果sentry为假,则不是。这是bool转换为27.6.2.3 [lib.ostream::sentry] p3 & p5的方式,取自ok_C 2b 2b ostream dev null test:

3) If, after any preparation is completed, os.good() is true, ok_ true otherwise, ok_ false.

5) operator bool();
Effects: Returns ok_.


(ostream::sentryDev

C%2b%2b Ostream Dev Null

bool类型的27.6.2.2 [lib.ostream.cons] p1的成员。)
请注意,这些引号在C++ 11中仍然存在,只是在不同的位置。为了在这个答案中出现:
  • 27.7.3.2 [ostream.cons] p1 =>27.4.4.1 [lib.basic.ios.cons] p3
  • 27.5.5.2 [basic.ios.cons] =>27.6.2.6 [lib.ostream.unformatted]
  • 表89 =>表128
  • 27.7.3.7 [ostream.unformatted] p1 =>27.6.2.3 [lib.ostream::sentry] p3 & p5
  • 27.7.3.4 [ostream::sentry] p4 & p5 => ojit_code
  • < 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
    Global objects
    Member functions
    (C++11)
    Formatted output
    Unformatted output
    Positioning
    Miscellaneous
    (C++11)
    Member classes
    Non-member functions
    basic_ostream& operator<<(short value );
    basic_ostream& operator<<(unsignedshort value );
    (1)
    basic_ostream& operator<<(int value );
    basic_ostream& operator<<(unsignedint value );
    (2)
    basic_ostream& operator<<(long value );
    basic_ostream& operator<<(unsignedlong value );
    (3)
    basic_ostream& operator<<(longlong value );
    basic_ostream& operator<<(unsignedlonglong value );
    (4) (since C++11)
    basic_ostream& operator<<(float value );

    basic_ostream& operator<<(double value );

    basic_ostream& operator<<(longdouble value );
    (5)
    (6)
    basic_ostream& operator<<(constvoid* value );
    (7)
    (8) (since C++17)
    basic_ostream& operator<<(std::basic_streambuf<CharT, Traits>* sb);
    (9)
    basic_ostream& operator<<(
    std::ios_base&(*func)(std::ios_base&));
    (10)
    basic_ostream& operator<<(
    std::basic_ios<CharT,Traits>&(*func)(std::basic_ios<CharT,Traits>&));
    (11)
    basic_ostream& operator<<(
    std::basic_ostream<CharT,Traits>&(*func)(std::basic_ostream<CharT,Traits>&));
    (12)

    Inserts data into the stream.

    1-2) Behaves as a FormattedOutputFunction. After constructing and checking the sentry object, if value is short or int, then casts it to unsignedshort or unsignedint if ios_base::flags()& ios_base::basefield is ios_base::oct or ios_base::hex. After that, casts to long in any case and outputs as in (3). If value is unsignedshort or unsignedint, casts to unsignedlong and outputs as in (3).
    3-7) Behaves as a FormattedOutputFunction. After constructing and checking the sentry object, inserts an integer, floating point, boolean or generic pointer value by calling num_put::put(). If the end of file condition was encountered during output (put().failed()true), sets ios::badbit.
    8) Outputs an implementation-defined string as if by *this << s, where s is a null-terminated character type string.
    9) Behaves as an UnformattedOutputFunction. After constructing and checking the sentry object, checks if sb is a null pointer. If it is, executes setstate(badbit) and exits. Otherwise, extracts characters from the input sequence controlled by sb and inserts them into *this until one of the following conditions are met:
    • end-of-file occurs on the input sequence;
    • inserting in the output sequence fails (in which case the character to be inserted is not extracted);
    • an exception occurs (in which case the exception is caught).

    C 2b 2b Ostream Dev Null Test

    If no characters were inserted, executes setstate(failbit). If an exception was thrown while extracting, sets failbit

    C 2b 2b Ostream Dev Null Command

    and, if failbit is set in exceptions()

    C 2b 2b Ostream Dev Null Code

    , rethrows the exception.
    10-12) Calls func(*this). These overloads are used to implement output I/O manipulators such as std::endl.

    [edit]Parameters

    value - integer, floating-point, boolean, or pointer value to insert
    func - function to call
    sb - pointer to the streambuffer to read the data from

    [edit]Return value

    1-11)*this

    [edit]Notes

    There are no overload for pointers to non-static member, pointers to volatile, or function pointers (other than the ones with signatures accepted by the (10-12) overloads). Attempting to output such objects invokes implicit conversion to bool, and, for any non-null pointer value, the value 1 is printed (unless boolalpha was set, in which case true is printed).

    Character and character string arguments (e.g., of type char or constchar*) are handled by the non-member overloads of operator<<. Attempting to output a character using the member function call syntax (e.g., std::cout.operator<<('c');) will call one of overloads (2-4) and output the numerical value. Attempting to output a character string using the member function call syntax will call overload (7) and print the pointer value instead.

    [edit]Example

    Output:

    C 2b 2b Ostream Dev Null Key

    [edit]See also

    inserts character data
    (function template)[edit]
    performs stream I/O of strings
    (function template)
    performs stream input and output of bitsets
    (function)
    serializes and deserializes a complex number
    (function template)
    performs stream input and output on pseudo-random number engine
    (function template)[edit]
    performs stream input and output on pseudo-random number distribution
    (function template)[edit]
    inserts a character
    (public member function)[edit]
    inserts blocks of characters
    (public member function)[edit]
    (C++17)
    converts an integer or floating-point value to a character sequence
    (function)[edit]
    Retrieved from 'https://en.cppreference.com/mwiki/index.php?title=cpp/io/basic_ostream/operator_ltlt&oldid=124218'