At the advice of a high rep SO user, I've recently started compiling with the -Wconversion
flag on my codebase. This has generated quite a few warnings, some which are legitimate (needlessly adding signed
and unsigned
types, for instance), but also some head scratchers, demonstrated below:
#include <cstdint>
int main()
{
uint16_t a = 4;
uint16_t b = 5;
b += a;
return 0;
}
When I compile with g++ -Wconversion -std=c++11 -O0 myFile.cpp
, I get
warning: conversion to 'uint16_t {aka short unsigned int}' from 'int' may alter its value [-Wconversion]
b += a;
^
I've perused some similar questions on SO (dealing with |
and <<
operators), taken a look here, and have read the Numeric Promotions and Numeric Conversions sections here. My understanding is, in order to do the math, a
and b
are promoted to int
(since that's the first type that can fit the entire uint16_t
value range), math is performed, the result is written back... except the result of the math is an int
, and writing that back to uint16_t
generates the warning. The consensus of the other questions was basically to cast away the warning, and the only way I've figured out how to do that is b = (uint16_t)(b + a);
(or the equivalent b = static_cast<uint16_t>(b + a);
).
Don't want this question to get too broad, but assuming my understanding of integer promotions is correct...
- What's the best way to handle this moving forward? Should I avoid performing math on types narrower than
int
? It seems quite odd to me that I have to cast an arithmetic result which is the same type as all the operands (guess I would expect the compiler to recognize that and suppress the warning). Historically, I've liked to use no more bits than I need, and just let the compiler handle the promotions/conversions/padding as necessary. - Anyone use
-Wconversion
flag frequently? Just after a couple of days of using it myself, I'm starting to think its best use case is to turn it on, look at what it complains about, fix the legitimate complaints, then turn it back off. Or perhaps my definition of "legitimate complaint" needs readjusting. Replacing all of my+=
operators with spelled out casts seems like a nuisance more than anything.
I'm tempted to tag this as c
as well, since an equivalent c
code compiled with gcc -Wconversion -std=c11 -O0 myFile.c
produces the exact same warning. But as is, I'm using g++
version 5.3.1 on an x86_64 Fedora 23 box. Please point me to the dupe if I've missed it; if the only answer/advice here is to cast away the warning, then this is a dupe.
-Wconversion
warnings, though, is unanswered, but worth answering here. (And C# is even more confusing).-Wconversion
differently, it doesn't warn for this case. I use this switch turned on all the time (I don't turn off, like you say in 2.), and I always fix the issues. But I rarely use shorter types for arithmetics. There's simply no reason for using them.g++
5.3.1 .. sorry put that all the way at the bottom. I don't have quick access to clang right now or I would've tried it there too. Interesting that it doesn't warn for this, geza, thanks for the info.