我终于在我的程序中找出了一个错误,它是由返回类型的隐式类型转换引起的。即使使用 g++ -Wall
也不会对此发出警告。
不知道有没有办法快速找出这种无脑错误?
#include <iostream>
// return type should be int, but I wrote bool by mistake
bool foo(int x) {
return x;
}
int main() {
for (int i = 0; i < 100; ++i) {
std::cout << foo(i) << std::endl;
// 0 1 1 1 1 1 ..
// should be 0 1 2 3 4 ...
}
return 0;
}
最佳答案
这是正确的代码。如果 (i) i
的类型 int
也是正确的。
n3376 4.12/1
A prvalue of arithmetic, unscoped enumeration, pointer, or pointer to member type can be converted to a prvalue of type bool. A zero value, null pointer value, or null member pointer value is converted to false; any other value is converted to true.
关于c++ - 为什么没有隐式类型转换的警告?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15135196/