I am writing some low level code for my emulator which involves a lot of 16 and 8 bit unsigned integers. I enabled -Wconversion
warning in my project and all warnings are considered as errors (-Werror
).
Consider this code snippet:
#include <cstdint>
int main ()
{
uint16_t a = 4;
uint16_t b = 6;
uint16_t c = a + b;
}
Until GCC 9.3 with -std=c++17 -Wconversion -Werror
as compilation flags, gives the following error:
<source>: In function 'int main()':
<source>:7:20: error: conversion from 'int' to 'uint16_t' {aka 'short unsigned int'} may change value [-Werror=conversion]
7 | uint16_t c = a + b;
| ~~^~~
But the same code does not give this error for GCC 10.1
, and for any compiler version of Clang
(Tested it until Clang 5.0.0
). Link to compiler explorer.
So my questions are the following:
- IMO, addition of two
unsigned ints
should not be implicitly converted to anint
. Or is my assumption wrong? - Why
Clang
andGCC
(until 9.3) produce different results? Does the standard stipulate any constraints on this operation or is it up to the compiler vendor to decide? - What changed in
GCC 10.1
? Why is this error not popping up forGCC 10.1
?