c++ - 错别字:Linux 下 std::ifstream 中 bool 到 int 转换引起的错误

标签 c++ language-lawyer ifstream

我有一个拼写错误(|| 而不是 |)并注意到这样的代码在 GCC 中失败并在 Visual 中编译。 我知道 std::ifstream 的第二个参数是一个 int。所以理论上,bool 必须隐式转换为 int。那么为什么会失败呢?

引发错误的示例(我只是使用了一些整数而不是标志)。

#include <fstream>

int main(int argc, char * argv[]) {
  std::ifstream("foo", 2 | 3 || 4)
}

最佳答案

std::ifstream's constructorstd::ios_base::openmode 作为第二个参数这是typedef根据实现定义类型编辑的:

typedef /*implementation defined*/ openmode;

看起来 Visual 使用整数,而 GCC 没有,这就是为什么您的代码在 GCC 上失败的原因。

关于c++ - 错别字:Linux 下 std::ifstream 中 bool 到 int 转换引起的错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47456543/

相关文章:

c++ - 模板类的嵌套类中的Friend运算符

c - 是否应该从 main() 返回或调用 exit() 禁用线程取消?

c++ - C++中的ifstream和ofstream错误

c++ - 模板参数推导 : which compiler is right here?

c++ - 文件描述符上的 IO

c++ - 将字符串转换为 double C++

c++ - swig : undefined symbol: _ZN7hosters11hostersLink7getLinkEi 错误

c++ - 关于fork系统调用和全局变量

c++ - 虚拟 COM 端口到 Socket 通信

c++ - 等式运算符重载 : Is (x! =y) == (!(x==y))?