我有一个拼写错误(||
而不是 |
)并注意到这样的代码在 GCC 中失败并在 Visual 中编译。
我知道 std::ifstream
的第二个参数是一个 int
。所以理论上,bool
必须隐式转换为 int
。那么为什么会失败呢?
引发错误的示例(我只是使用了一些整数而不是标志)。
#include <fstream>
int main(int argc, char * argv[]) {
std::ifstream("foo", 2 | 3 || 4)
}
最佳答案
std::ifstream
's constructor将 std::ios_base::openmode
作为第二个参数这是typedef
根据实现定义类型编辑的:
typedef /*implementation defined*/ openmode;
看起来 Visual 使用整数,而 GCC 没有,这就是为什么您的代码在 GCC 上失败的原因。
关于c++ - 错别字:Linux 下 std::ifstream 中 bool 到 int 转换引起的错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47456543/