使用 g++ -std=c++17 -Wall -pedantic main.cpp
编译此代码不会产生任何警告:
#include <iostream>
#include <stdlib.h>
int main(int argc, char const *argv[]) {
for (int i = 0; i < 100; ++i) {
float x = 300.0 + rand();
char c = x;
std::cout << c << std::endl;
}
return 0;
}
它不应该产生缩小误差吗?
最佳答案
我做了一些研究,发现 -Wall
不会警告类型转换问题。
相反,使用标志 -Wconversion
以获得有关潜在类型转换问题的警告。
备注:
对于VC++的用户,/W4
会在类型转换时警告你可能丢失数据
关于c++ - 为什么 C++ 在将 float 转换为 char 时不显示缩小转换错误?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52443036/