2014-10-30 51 views
1

如何强制gcc/clang在此类代码中显示有关将double转换为int的警告(特别是在使用std :: accumulate作为double类的容器但结果为整数时):std :: accumulate中的double to int转换警告

#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报告模板功能中的类型扣减警告,您可以通过该警告捕获错误。

回答

0

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

+0

如果我能解释downvote,将不胜感激。 – 0x499602D2 2014-10-31 20:02:39