对我来说,这是一个真正的WTF,看起来像是GCC中的错误,但是我希望让社区来看看,并为我找到解决方案。
这是我可以召集的最简单的程序:
#include
#include
int main(void)
{
uint16_t i = 1;
uint16_t j = 2;
i += j;
return i;
}
我正在尝试使用i = (uint16_t)(i +3);标志在GCC上对此进行编译,这在我的大部分代码中都使用了。
结果如下:
.code.tio.c: In function ‘main’:
.code.tio.c:9:7: error: conversion to ‘uint16_t {aka short unsigned int}’ from ‘int’ may alter its value [-Werror=conversion]
i += j;
此代码将发生相同的错误:
uint16_t i = 1;
i += ((uint16_t)3);
错误是
.code.tio.c: In function ‘main’:
.code.tio.c:7:7: error: conversion to ‘uint16_t {aka short unsigned int}’ from ‘int’ may alter its value [-Werror=conversion]
i += ((uint16_t)3);
^
只需清楚一点,这里的错误是在i = (uint16_t)(i +3);运算符上,而不是强制转换上。
看起来i = (uint16_t)(i +3);和uint16_t的操作员重载被搞砸了。 还是我在这里想念些微妙的东西?
供您使用:MCVE
编辑:更多相同的:
.code.tio.c:8:6: error: conversion to ‘uint16_t {aka short unsigned int}’ from ‘int’ may alter its value [-Werror=conversion]
i = i + ((uint16_t)3);
但是i = (uint16_t)(i +3);至少可以工作...