为什么 C++ 标准库中 std::fstream 类的成员函数 open 返回 void 而不是 bool 来立即检查是否成功打开?
最佳答案
因为如果设置了异常,则当 open
调用不成功时它会抛出异常,或者它们使用 failbit
来指示发生错误。来自文档:
On failure, the failbit flag is set (which can be checked with member fail), and depending on the value set with exceptions an exception may be thrown.
关于c++ - 为什么 std::fstream 返回 void 而不是 bool,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12716151/