This is a real WTF for me, looks like a bug in GCC, but I'd like to have the community have a look and find a solution for me.
Here's the simplest program I could muster:
#include <stdio.h>
#include <stdint.h>
int main(void)
{
uint16_t i = 1;
uint16_t j = 2;
i += j;
return i;
}
I'm trying to compile this on GCC with -Werror=conversion
flag, which I'm using for much of my code.
Here's the result:
.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;
Same error would happen for this code:
uint16_t i = 1;
i += ((uint16_t)3);
Error is
.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);
^
Just to be clear, the error here is on the +=
operator, NOT the cast.
It looks like the operator overloading for the +=
with uint16_t
is messed up. Or am I missing something subtle here?
For your use: MCVE
Edit: Some more of the same:
.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);
But i = (uint16_t)(i +3);
at least works...