[英]double to int conversion warning in std::accumulate
How to force the gcc/clang to show a warning about converting double to int in such code (in particular when using std::accumulate for container of doubles but have result in integer): 如何在这样的代码中强制gcc / clang显示有关将double转换为int的警告(尤其是在对double容器使用std :: accumulate但结果为整数时):
#include <iostream>
#include <numeric>
#include <vector>
int main() {
std::vector<double> v = { 0.5, 0.6, 0.7 };
// gives wrong result due integer initial value
std::cout << std::accumulate( v.begin(), v.end(), 0 ) << std::endl; // no warning
int i = 4.2; // warning
return 0;
}
-Wconversion does not work well. -Wconversion不能很好地工作。 Link: http://goo.gl/efJUou
链接: http : //goo.gl/efJUou
PS VS2013 reports about type deduction warning in template function, by which you can catch the error. PS VS2013报告有关模板功能中的类型推断警告的信息,通过它可以捕获错误。
Warnings are disabled by default for system headers. 默认情况下,系统标头禁用警告。 You can use the option
-Wsystem-headers
to enable them, but the downside is that it will output additional irrelevant warnings. 您可以使用选项
-Wsystem-headers
启用它们,但缺点是它将输出其他不相关的警告。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.