std :: accumulate中的double to int conversion警告

时间:2014-10-30 16:16:21

标签: c++ type-conversion compiler-warnings

如何强制gcc / clang显示有关在此类代码中将double转换为int的警告(特别是当使用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效果不佳。链接:http://goo.gl/efJUou

P.S。 VS2013报告模板功能中的类型扣除警告,您可以通过它捕获错误。

1 个答案:

答案 0 :(得分:0)

系统标头默认禁用警告。您可以使用选项-Wsystem-headers启用它们,但缺点是它会输出其他不相关的警告。

相关问题