[Bug c++/58875] New: No float to int conversion warning in std::inner_product(x, x_end, y, 0)
yangzhe1990 at gmail dot com
gcc-bugzilla@gcc.gnu.org
Fri Oct 25 10:33:00 GMT 2013
http://gcc.gnu.org/bugzilla/show_bug.cgi?id=58875
Bug ID: 58875
Summary: No float to int conversion warning in
std::inner_product(x, x_end, y, 0)
Product: gcc
Version: 4.8.1
Status: UNCONFIRMED
Severity: normal
Priority: P3
Component: c++
Assignee: unassigned at gcc dot gnu.org
Reporter: yangzhe1990 at gmail dot com
The correct code should be std::inner_product(x, x_end, y, 0.0), but with
-Wconversion enabled, there should be a float to int conversion warning.
Without the warning it's easy to write the wrong code and get the wrong answer.
For example, compiling
#include <algorithm>
#include <iostream>
int main() {
float x[3] = {0.5, 0.5, 0.5};
std::cout << std::inner_product(x, x + 3, x, 0) << std::endl;
int y = 0;
y += x[1];
return 0;
}
generates the following warnings,
test.cpp: In function ‘int main()’:
test.cpp:8:4: warning: conversion to ‘float’ from ‘int’ may alter its value
[-Wconversion]
y += x[1];
^
test.cpp:8:4: warning: conversion to ‘int’ from ‘float’ may alter its value
[-Wconversion]
But a warning to the inner_product line is expected.
More information about the Gcc-bugs
mailing list