2014-10-30 55 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