You can then use this buffer in any ostream class. NullBuffer nullbuffer; std::ostream nullstream(&nullbuffer); nullstream. Lo standard garantisce che questo 27.6.2.2 lib.ostream.cons p1, a partire da 27.6.2.2 lib.ostream.cons p1 che descrive il costruttore di ostream che accetta un puntatore a uno streambuf: Effetti: basicostream un object di class basicostream, assegnando i valori iniziali alla class base chiamando basicios::init(sb). An Ostream is an abstract base class for all output systems (streams, files, token lists.
这个问题已经在这里有了答案:
9年前关闭。
Possible Duplicate:
Implementing a no-op std::ostream
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] p3
,badbit
的相关功能:void init(basic_streambuf<charT,traits>* sb);
Postconditions: The postconditions of this function are indicated in Table 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_
:3) If, after any preparation is completed, os.good()
is true
, ok_ true
otherwise, ok_ false
.
5) operator bool();
Effects: Returns ok_.
ostream::sentry
是bool
类型的27.6.2.2 [lib.ostream.cons] p1
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]
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 这个问题已经在这里有了答案:
9年前关闭。
Possible Duplicate:
Implementing a no-op std::ostream
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] p3
,badbit
的相关功能:void init(basic_streambuf<charT,traits>* sb);
Postconditions: The postconditions of this function are indicated in Table 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_
:3) If, after any preparation is completed, os.good()
is true
, ok_ true
otherwise, ok_ false
.
5) operator bool();
Effects: Returns ok_.
ostream::sentry
是bool
27.6.2.2 [lib.ostream.cons] p1
的成员。)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]
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