平台无关/ dev / null在c ++ [英] Platform independent /dev/null in c++
问题描述
可能重复:
实施no-op std :: ostream
在c ++中有没有相当于NULL的流?我想写一个函数,如果用户想要将内部输出到某个地方,但如果没有,输出会进入一些假的地方,
Is there any stream equivalent of NULL in c++? I want to write a function that takes in a stream if the user wants to have the internal outputted to somewhere, but if not, the output goes into some fake place
void data(std::stream & stream = fake_stream){
stream << "DATA" ;
}
我想要能够选择 ()
或 data(std :: cout)
推荐答案
编辑:取自@Johannes Schaub - litb的邮件这里,稍加修改:
Edit: Taken from @Johannes Schaub - litb's mail here with slight modifications:
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
// in some .h
extern std::ostream cnull;
extern std::wostream wcnull;
// in a concrete .cpp
nullbuf null_obj;
wnullbuf wnull_obj;
std::ostream cnull(&null_obj);
std::wostream wcnull(&wnull_obj);
使用这些:
void data(std::ostream& stream = cnull){
// whatever...
}
现在,这看起来很酷和所有,但下面是更短和工作,因为如果一个空指针提供给 ostream
的构造函数,它会自动设置badbit,并默默忽略任何写入:
Now, this looks cool and all, but the following is way shorter and works, because if a null pointer is provided to the constructor of ostream
, it automatically sets the badbit and silently ignores any writes:
// in .h
extern std::ostream cnull;
extern std::wostream wcnull;
// in .cpp
std::ostream cnull(0);
std::wostream wcnull(0);
标准保证这个工作,从 27.6.2.2 [lib.ostream .cons] p1
其中描述了 ostream
的构造函数,它接受一个指向 streambuf
:
The standard guarantees this works, beginning from 27.6.2.2 [lib.ostream.cons] p1
which describes the constructor of ostream
that takes a pointer to a streambuf
:
效果:构造类
basic_ostream
的一个对象,基本类通过调用basic_ios< charT,traits> :: init(sb)
。
Effects: Constructs an object of class
basic_ostream
, assigning initial values to the base class by callingbasic_ios<charT,traits>::init(sb)
.
basic_ios
, 27.4.4.1 [lib.basic.ios.cons] p3
:
void init(basic_streambuf< charT,traits> * sb);
后置条件:此函数的后置条件如表89所示:
void init(basic_streambuf<charT,traits>* sb);
Postconditions: The postconditions of this function are indicated in Table 89:
表89中的重要行:
rdstate() - goodbit if sb不是空指针,否则为badbit。
rdstate() -- goodbit if sb is not a null pointer, otherwise badbit.
如果 badbit
被设置,会在 27.6.2.6 [lib.ostream.unformatted]
:
每个未格式化的输出函数通过构造
sentry
。如果此对象返回true,则在转换为bool类型的值时,该函数会尝试生成请求的输出。
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
为false,则不会。下面是 sentry
如何转换为 bool
,取自 27.6.2.3 [lib.ostream :: sentry] p3& p5
:
This implies that, in case the sentry
is false, it does not. Here is how the sentry
converts to bool
, taken from 27.6.2.3 [lib.ostream::sentry] p3 & p5
:
3)如果在任何准备工作完成后,
os.good ()
是true
,ok_ == true
ok_ == false 。
3) If, after any preparation is completed,
os.good()
istrue
,ok_ == true
otherwise,ok_ == false
.
5) operator bool();
效果:返回确定_。
5) operator bool();
Effects: Returns ok_.
( ok _
ostream :: sentry 类型
bool
。)
请注意,这些引号仍然存在于C ++ 11中,只是在不同的地方。按照此回答中的出现顺序:
Note that these quotes are still present in C++11, just in different places. In order of appearance in this answer:
-
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]
- 表89 =>表128
-
27.6.2.6 [lib.ostream.unformatted]
=>.3.7 [ostream.unformatted] p1
-
27.6.2.3 [lib.ostream :: sentry] p3& p5
=>27.7.3.4 [ostream :: sentry] p4& p5
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]
- Table 89 => Table 128
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
这篇关于平台无关/ dev / null在c ++的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!