std::basic_ios::operator bool
来自cppreference.com
该页由英文版wiki使用Google Translate机器翻译而来。
该翻译可能存在错误或用词不当。鼠标停留在文本上可以看到原版本。你可以帮助我们修正错误或改进翻译。参见说明请点击这里. |
operator void*() const; |
(1) | (至 C++11) |
explicit operator bool() const; |
(2) | (C++11 起) |
fail()回报true,否则返回一个非空指针,则返回一个空指针。这个指针是隐式转换为bool,可用于在布尔上下文中.....
2) 原文:
Returns a null pointer if fail() returns true, otherwise returns a non-null pointer. This pointer is implicitly convertible to bool and may be used in boolean context.
返回true如果流没有发生任何错误的I / O操作做好准备。具体而言,回报!fail().
原文:
Returns true if the stream has no errors occurred and is ready of I/O operations. Specifically, returns !fail().
经营者可以使用流和函数返回引用流作为循环条件,在地道的C + +输入回路,如while(stream >> value) {...}或while(getline(stream, string)){...}。这样的循环执行循环体,只有当输入操作成功.
原文:
This operator makes it possible to use streams and functions that return references to streams as loop conditions, resulting in the idiomatic C++ input loops such as while(stream >> value) {...} or while(getline(stream, string)){...}. Such loops execute the loop's body only if the input operation succeeded.
目录 |
[编辑] 参数
(无)
[编辑] 返回值
true如果流没有发生任何错误,false否则.
原文:
true if the stream has no errors occurred, false otherwise.
[编辑] 示例
#include <iostream> #include <sstream> int main() { std::istringstream s("1 2 3 error"); int n; std::cout << std::boolalpha << "(bool)s is " << (bool)s << '\n'; while (s >> n) { std::cout << n << '\n'; } std::cout << std::boolalpha << "(bool)s is " << (bool)s << '\n'; }
输出:
(bool)s is true 1 2 3 (bool)s is false
[编辑] 另请参阅
ios_base::iostate flags | basic_ios accessors | |||||||
eofbit | failbit | badbit | good() | fail() | bad() | eof() | operator bool() | operator!() |
false | false | false | true | false | false | false | true | false |
false | false | true | false | true | true | false | false | true |
false | true | false | false | true | false | false | false | true |
false | true | true | false | true | true | false | false | true |
true | false | false | false | false | false | true | true | false |
true | false | true | false | true | true | true | false | true |
true | true | false | false | true | false | true | false | true |
true | true | true | false | true | true | true | false | true |