if (fstreamTest) 检查什么?它正在检查特定标志吗?

What does if (fstreamTest) check for? Is it checking for a specific flag?

在下面的代码中

fstream testFile;
testFile.open("test.txt", ios::in);

if (testFile)
cout << "This if statement is true";

为了从 if(testfile) return true,C++ 检查什么? 是否检查 testFile.goodbit() 是否为真?

您可以查找定义... http://en.cppreference.com/w/cpp/io/basic_ios/operator_bool or http://www.cplusplus.com/reference/ios/ios/operator_bool/ 如前所述。最后,它在返回 true 或 false 的 fstream class 上调用一个方法。

该方法的文档说:

Returns true if the stream has no errors occurred and is ready
of I/O operations. Specifically, returns !fail()

另一个版本的文档说:

Returns whether an error flag is set (either failbit or badbit).

Notice that this function does not return the same as member good,
but the opposite of member fail.

What does C++ check for in order to return true from if(testfile)?

来自 cppreference:

Checks whether the stream has no errors.

1) 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 contexts. (until C++11)

2) Returns true if the stream has no errors and is ready for I/O operations. Specifically, returns !fail(). (since C++11)

1) operator void*() const;直到 C++11)
2) explicit operator bool() const; (因为 C++11)

如果流没有错误,它将 return true,否则它将 return false.

这使得可以执行以下操作:

while(stream >> value) {
    ...
}