c++ 中的平台独立 /dev/null
可能的重复:
实现无操作 std::ostream
在 c++ 中是否有与 NULL 等效的流?我想编写一个函数,如果用户希望将内部输出到某个地方,则该函数会接收流,但如果没有,输出会进入一些虚假的地方,
void data(std::stream & stream = fake_stream){
stream << "DATA" ;
}
我希望能够选择执行 data()< /code> 或
data(std::cout)
Possible Duplicate:
Implementing a no-op std::ostream
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" ;
}
i want to be able to chose to do data()
or data(std::cout)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
编辑:摘自 @Johannes Schaub - litb 的邮件 这里稍作修改:
使用这些:
现在,这看起来很酷,但是下面的代码更短并且有效,因为如果向构造函数提供空指针
ostream
,它会自动设置 badbit 并默默地忽略任何写入:标准保证了这一点,从描述构造函数的
27.6.2.2 [lib.ostream.cons] p1
开始的ostream
,它接受一个指向streambuf
的指针:来自
basic_ios
,27.4.4.1 [lib.basic.ios.cons] p3
的相关函数:表 89 中的重要行:
27.6.2.6 [lib.ostream.unformatted]
中描述了如果设置badbit
会发生什么:这意味着,如果
sentry
为 false,则它不会。以下是sentry
如何转换为bool
,取自27.6.2.3 [lib.ostream::sentry] p3 & p5:
(
ok_
是ostream::sentry
类型为bool
的成员。)请注意,这些引号在 C++11 中仍然存在,只是在不同的地方。按照此答案中出现的顺序:
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
Edit: Taken from @Johannes Schaub - litb's mail here with slight modifications:
Use those:
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:The standard guarantees this works, beginning from
27.6.2.2 [lib.ostream.cons] p1
which describes the constructor ofostream
that takes a pointer to astreambuf
:The relevant function from
basic_ios
,27.4.4.1 [lib.basic.ios.cons] p3
:The important row from Table 89:
What happens if the
badbit
is set is described under27.6.2.6 [lib.ostream.unformatted]
:This implies that, in case the
sentry
is false, it does not. Here is how thesentry
converts tobool
, taken from27.6.2.3 [lib.ostream::sentry] p3 & p5
:(
ok_
is a member ofostream::sentry
of typebool
.)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]
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
Linux 文件 /dev/null 是一个像你正在寻找的黑洞。在 Windows 中,有一个名为 NUL: 的设备。我从未尝试过打开该文件,但我从命令行使用过它
Linux file /dev/null is a black hole like you're looking for. In Windows there's a device called NUL:. I've never tried to open that file, but I've used it from the command line
你可以尝试 ostream(NULL,false),第一个输入是目标输出,我不知道第二个输入的确切含义,但在跟踪代码后,似乎只是因为 ostream 没有地方可写,调用
operator < ;<
被 ostream 忽略。我的意思是,在第一次调用状态更改为坏,之后它总是忽略输入数据,因为流状态,所以你可以使用以下代码:you can try ostream(NULL,false), the first input is target output and I don't know what the second input exaclty mean but after tracing code it seems just because ostream has no place to write to, calling
operator <<
is just ignored by ostream. I mean in the first call state changes to bad and after that it's always ignoring input data because of stream state ,so you can use the following code :