NVCC Odd Warning, without warning number

Morning! We’re very close to zero warnings on our codebase , but have one that keeps coming back from NVCC.

Specifically, the warning comes around this bit of code in the boost MPL library:

#elif defined(BOOST_MSVC)
    enum { n = sizeof(T) + -1 };

And yields this (rather cryptic) message: boost/mpl/print.hpp(55): warning : integer conversion resulted in a change of sign

As there isn’t a warning number around this thing, I can’t suppress it - two questions:

  1. What is this warning telling me?
  2. How do I make it go away?

Making the cast explicit seems to fix it - but what does change of sign mean about this - what’s the warning telling me?

#elif defined(BOOST_MSVC)
    enum { n = (int)sizeof(T) + -1 };

sizeof(T) returns an unsigned integer type. In the following addition -1 is therefore converted to this unsigned type (that is, it becomes a positive integer), before the two operands are added. The result may therefore differ from what the programmer expected or intended.

This is all standard C/C++ type conversion behavior. The compiler is nice enough to warn about it, rather than have a programmer fall into the signed/unsigned conversion trap and spend hours finding the root cause of their buggy code. The CUDA compiler may warn more frequently about these cases than other compilers, since it appears to alert on any sign change caused by a default integer conversion, rather than only those which can cause problems; in this case I do not see how the conversion will change the overall semantics of the assignment, but I may be overlooking something.

In general, mixing of signed and unsigned integers in expressions (including comparisons) should be avoided, as it often leads to unintended semantics that are the source of hard to find bugs. Been there, done that, got the t-shirt.

wince C-style casts!

Turn on -Wold-style-casts