如何std :: ostringstream转换为bool? [英] How would std::ostringstream convert to bool?
问题描述
我偶然发现了这个代码。
I stumbled across this code.
std::ostringstream str;
/// (some usage)
assert( ! str );
ostringstream
在 bool
上下文中使用 ?
推荐答案
它会告诉你流是否当前有效。这是所有流可以做的事情。例如,如果文件未正确打开,则文件流可能无效。
It tells you if the stream is currently valid. This is something that all streams can do. A file stream, for example, can be invalid if the file was not opened properly.
另外,这个功能(将流测试为bool)通过在C ++ 11及更高版本中重载显式运算符bool
并重载 void *
As a side note, this functionality (testing a stream as a bool) is achieved by overloading explicit operator bool
in C++11 and later and by overloading the void*
cast operator in versions before C++11.
这里是一个包含一些流为什么可能失败的例子。
编辑:已更改 bool code>到
void *
后,马丁·约翰指出我的错误。
changed bool
to void*
after Martin York pointed out my mistake.
这篇关于如何std :: ostringstream转换为bool?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!