这对我来说是一个真正的WTF,看起来像GCC中的一个bug,但我想让社区看看,为我找到一个解决方案.
下面是我能想到的最简单的程序:
#include <stdio.h>
#include <stdint.h>
int main(void)
{
uint16_t i = 1;
uint16_t j = 2;
i += j;
return i;
}
我试着用-Werror=conversion
旗在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);
^
为了清楚起见,这里的错误出现在+=
运算符上,而不是强制转换.
看起来+=
号和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);
元还能用……