Let's compile the following program:
int main()
{
uint16_t data = 0;
data |= uint16_t(std::round(3.14f));
return 0;
}
with g++ -Wconversion prog.cpp
We'll get warning: conversion to ‘uint16_t {aka short unsigned int}’ from ‘int’ may alter its value, but I can't see implicit conversions here.
This kind of warnings should be muted by explicit casts, for example:
double d = 3.14;
float foo1 = d; // Warning
float foo2 = float(d); // No warning
float foo2 = static_cast<float>(d); // No warning
Is GCC right here or it's a bug?
Note that my snippet is minimal. For example, warning disappears in following cases:
- remove the
fsuffix from3.14, i.e. make itdouble - use assignment instead of
|= - remove
std::round - cache rounding result:
const auto r = uint16_t(std::round(3.14f));, then or-assign it todata.