如何将标准:: ostringstream转换为bool? [英] How would std::ostringstream convert to bool?
问题描述
我碰到这个code绊倒了。
I stumbled across this code.
std::ostringstream str;
/// (some usage)
assert( ! str );
这是什么 ostringstream
表示在 使用时BOOL
背景?
What does ostringstream
signify when used in a bool
context?
这是可能发生这种情况编译和运行一个不正确的使用情况如何?
Is this possibly an incorrect usage that happens to compile and run?
推荐答案
它会告诉你,如果流目前是否有效。这一点,所有的数据流可以做到。的文件流,例如,如果文件没有被正确打开可以是无效的。
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)是由超载达到明确的运营商BOOL
的在C ++ 11及更高版本,并在版本超载无效*
转换运算符的之前,C ++ 11 的
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.
下面是包含的为什么流可能会失败的一些例子。这不是特定字符串流,但是它确实适用于他们。
Here is a link containing some examples of why a stream might fail. This isn't specific to string streams, but it does apply to them.
编辑:修改布尔
到无效*
之后,马丁纽约指出我的错误
changed bool
to void*
after Martin York pointed out my mistake.
这篇关于如何将标准:: ostringstream转换为bool?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!