如何将标准:: ostringstream转换为bool? [英] How would std::ostringstream convert to bool?

查看:184
本文介绍了如何将标准:: ostringstream转换为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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆