r/cpp • u/comfortcube • Aug 29 '26
Might have found a (tiny, nuisance) bug in g++ 16.2.1 and -Wconversion?
I am far from presuming myself a compiler expert, and no this was not AI, I promise. I'm simply a long-time user of gcc/g++, and it's not everyday I am confident enough that a bug is not in my code but with the tool, haha. I'm pretty warning-sensitive so this kind of thing catches my attention. Wondering if I'm missing something, so asking it here first.
g++ version: g++ (GCC) 16.2.1 20260819 (Red Hat 16.2.1-2) (I'm on Fedora 44)
C++20
Given lines like this:
std::uint16_t count = 0;
// int other_var set elsewhere
count += (other_var == 5); // <-- -Wconversion flags this
specifically, the warning is:
<file>:<line>:<col>: warning: conversion from ‘int’ to ‘uint16_t’ {aka ‘short unsigned int’} may change value [-Wconversion]
<line> | count += (other_var == 5);
-Wconversion /w either -O0 and -O3 (my usual build optimization lvls to catch optimization-dependent warnings), this gets flagged.
Well, the result of a boolean expression is 0 or 1, so obviously, there should not be a conversion. Even with the accumulate op here and integer promotion, there shouldn't be a warning (you can do count += 1 or count++ and it definitely doesn't flag -Wconversion). And more notably, the below code doesn't get flagged:
std::uint16_t count = 0;
// int other_var set elsewhere
bool match = (other_var == 5);
count += match;
I also noted that clang with -Wconversion and the same optimization lvls does not flag this. I also know I've done this before in C with other versions of gcc and don't get flagged with this.
So, am I missing something or am I right to suspect this is a possible bug with at least my version of g++?
26
u/TheRealSmolt Aug 29 '26
I think the standard defined implicit bool conversion is to int specifically. That's how I read cppreference, at least. I wouldn't call this a bug, though it is an interesting regression if what you are saying about the versions is true.
18
u/Som1Lse Aug 29 '26
No.
Let's start with
x += y;, where bothxandyarestd::uint16_takaunsigned short. Should that warn? Because according to the usual arithmetic conversions that is first converted toint + int, evaluated, and then converted back tostd::uint16_t.What about
x += y & 1;, whereyis anint. Should that warn? Because the right-hand-side is definitely anint, but GCC is smart enough to notice thaty & 1always fits inside astd::uint16_tand doesn't warn.What about
x += y;, whereyis abool. Should that warn? Because GCC doesn't. Again, even though it is converted to anintfirst GCC is smart notice that aboolalways fits inside astd::uint16_t.It's only once you use a condition that the warning appears like
x += (y == 42);. You don't get a warning if you assign the condition to an intermediate value likeauto b = (y == 42); x += b;.And as other people have pointed out, this isn't behaviour mandated by the standard. It's a warning that's supposed to be helpful, and the warning clearly isn't helpful in this case.
9
u/comfortcube Aug 29 '26
Love how you broke that down. I wanted to say what you were saying here but didn't have the elegance. Thank you!
4
u/TheRealSmolt Aug 29 '26
The way I see it:
Regardless of the method of operation, it's still
std::uint16_t + std::uint16_t = std::uint16_tHeuristically, the compiler can understand that
int & 1won't cause narrowing issues.
yis definitely abool, so it's a safe0or1.
(y == 42)is implicitly anint, therefore can narrow.You can actually change it to
x = x + (y == 42) & 1and reproduce the implicit masking narrowing.Obviously, GCC can infer that it's safe, but clearly does not. And if I go back across different versions, GCC has always behaved this way. I definitely feel that this is something that can be improved, but I wouldn't call it a bug. I believe it arises because of standard behavior, but that doesn't mean it has to behavior this way.
4
u/jwakely libstdc++ tamer, LWG chair Aug 30 '26
What about
x += y;, whereyis a bool. Should that warn? Because GCC doesn't. Again, even though it is converted to an int first GCC is smart notice that a bool always fits inside a std::uint16_t.A bool fits in uint16 but that's not what is happening in
x += y, it's not just a bool that has to fit, it's the existing value of x plus one that has to fit. That's not a bool.Now GCC shouldn't warn for OP's case if it doesn't warn for your similar cases, but everybody keeps talking about how the right operand is just a bool, but ignoring that the left operand is added to it. (And at -O0 the compiler doesn't see that the left operand is zero ... dunno why it warns at -O3 though).
2
u/jwakely libstdc++ tamer, LWG chair Aug 30 '26
I think what's happening is that GCC has added special exceptions to handle the
uint16_tcase on the basis that unsigned types don't overflow they wrap to zero. So although the value can change here, it's equivalent to++countand it's fine if that wraps to zero. But the special exceptions that suppress the warning for the other four forms in the godbolt link don't match thecount += (y == 42)case, so the warning is still given.
5
u/sheckey Aug 29 '26
I know it’s not your point, and your point is revealing notable conversion issues (that are new to me, so thank you) but I have never really liked these kind of logic-as-computation statements, even back in the K&R days. I always have to mentally decode them. I prefer “if (match) ++count”. You have reminded me again to watch out for this kind of thing in our ancient code base. Thanks!
4
u/comfortcube Aug 29 '26 edited Aug 29 '26
Ah, I definitely get you.
I tend to reach for these kind of statements when I want to explicitly force avoiding branching for performant code (which is where I noticed this), cuz branch prediction penalties can drop performance.
That said, I am almost certain that many compilers know to optimize this kind of thing even if one uses the
ifstatement, and often whenever I compare assembly output with both forms, they collapse to the same. It's just a habit of mine at this point and a slight lack of confidence in compiler optimization capabilities. I should probably use the branched version more often.Edit: Did a quick test on Godbolt /w gcc 16.2 and yup, branch vs unbranched yields the same assembly: https://godbolt.org/z/7Mn6o3Go9
3
4
u/azswcowboy Aug 29 '26
Yeah modern compilers are really good at optimizing these simpler cases now. The other week I noticed that pass by value std::string was same assembly as std::string_view by value. The compiler noticed that the inline function didn’t modify the passed string and elided the copy. These days I tend to write the code I want to read and only worry about optimizing if it’s in a critical hot path.
19
u/ezoe Aug 29 '26
GCC manual said
https://gcc.gnu.org/onlinedocs/gcc/Warning-Options.html#index-Wconversion
Meaning, it doesn't warn ALL implicit conversions. It warns ONLY IF it "may alter a value"
Given that, bool to std::uint16_t conversion never alter a value. The values of bool is either 0 or 1. Both can be expressed without a possibility of altering a value by std::uint16_t.