gio*_*adi 7 c++ gcc clang implicit-conversion
请考虑以下代码:
void f(int x)
{
std::cout << x << std::endl;
}
int main()
{
double x = 1.5;
f(x);
return 0;
}
Run Code Online (Sandbox Code Playgroud)
这编译并运行时没有任何警告(使用-Wall),因此隐藏了从double到int的危险隐式转换.如果使用文字调用函数,编译器将捕获强制转换,即
f(1.5)
Run Code Online (Sandbox Code Playgroud)
但这并不是那么有用.为什么这些编译器没有为这个演员发出警告?我在OSX 10.8.3上使用gcc-4.2.1和clang-425.0.28.
gio*_*adi 12
后人:
为了避免隐式转换,请使用-Wconversion标志(它不包含在-Wall中).Clang实际上在-Weverything标志中包含-Wconversion,但是这个标志比大多数用户习惯的警告要多得多.