gpt4 book ai didi

c++ - std::accumulate 中的 double 到 int 转换警告

转载 作者:太空宇宙 更新时间:2023-11-04 13:44:00 25 4
gpt4 key购买 nike

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

#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

附言VS2013报模板函数中的类型推导警告,可通过该警告捕获错误。

最佳答案

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

关于c++ - std::accumulate 中的 double 到 int 转换警告,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26658103/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com