简体   繁体   English

将std :: accumulate中的double转换为int转换警告

[英]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.

相关问题 MSVC 中从 _Ty 到 int 警告的转换累积 - Conversion from _Ty to int warning in MSVC accumulate std::accumulate 溢出警告 - Warning for overflow in std::accumulate 警告:从&#39;long int&#39;转换为&#39;double&#39;可能会改变其值 - warning: conversion to 'double' from 'long int' may alter its value 警告:“从&#39;double&#39;转换为&#39;int&#39;,可能会丢失数据” - Warning: “conversion from 'double' to 'int', possible loss of data” 使用 std::accumulate(v.begin(), v.end(), 0) 发出警告; - warning using std::accumulate(v.begin(), v.end(), 0); 为什么std :: accumulate函数显示向量的错误总和 <double> ? - Why is the std::accumulate function showing the wrong sum of a vector<double>? C ++将输入字符串与字符串进行比较会给我一个从双精度到整数转换的警告-可能会丢失数据 - C++ comparing input string with a string gives me a warning of conversion from double to int - possible data loss 警告C4244:&#39;argument&#39;:从&#39;double&#39;转换为&#39;const int&#39;,可能会丢失数据 - warning C4244: 'argument' : conversion from 'double' to 'const int', possible loss of data int到float转换会产生警告吗? - int to float conversion produces a warning? 警告C4244:“参数”:从“双精度”转换为“整数”,可能丢失数据 - Warning C4244: 'argument' : conversion from 'double' to 'int', possible loss of data
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM