I getting below warning. How to suppress this warning?
Warning:
warning: comparison of integer expressions of different signedness: 'DWORD' {aka 'unsigned int'} and 'int' [-Wsign-compare]
Code:
DWORD exp = GetExp();
int amount = 20;
if (amount < 0 && exp < -amount)
{
...
}
If you really want to suppress it (it's not recommended, there is a reason a warning is shown, and you should instead eliminate the reason), you can do it by adding -Wno-sign-compare to the gcc command line.
Related
I compiled the following code with -Wsign-conversion :
int main()
{
unsigned int a = 8;
int b = a + 8u; // warning: implicit conversion changes signedness: 'unsigned int' to 'int'
int c = a - 8u; // warning: implicit conversion changes signedness: 'unsigned int' to 'int'
int d = a * 8u; // warning: implicit conversion changes signedness: 'unsigned int' to 'int'
int e = a / 8u; // gcc warns, but no warning in clang
}
Clang doesn't emit a warning when assigning from the result of an unsigned division, but gcc does.
Why is there a difference in this one particular case?
Clang is just being an extra bit clever: division of an unsigned integer by an unsigned integer larger or equal to 2 means the unsigned integer result will always fit in the signed integer counterpart (to that of the numerator type in the division expression). However, if you divide by an unsigned integer valued 1, the result is no longer guaranteed to fit in a signed integer counterpart, and Clang does emit a warning:
#include <cstdint>
int main() {
uint8_t a = 240; // '240 / 1' will not fit in int8_t
int8_t e = a / 2u; // No warning in clang
int8_t f = a / 1u; // warning: implicit conversion changes signedness: 'unsigned int' to 'int8_t' (aka 'signed char') [-Wsign-conversion]
}
One could also argue that Clang should be able to omit the warning for the similar special case of multiplication by 0; however Clang does not whereas GCC, instead, does:
// Clang warns, no warning in GCC.
int8_t g = a * 0u;
So peculiarly Clang is clever, in this context, w.r.t. division and GCC w.r.t. multiplication.
Finally, note that the gating for Clang to emit this warning during division seems to be only when dividing by 1, as you will not get the same -Wsign-conversion if you divide by 0u; arguably as it has been overridden by the more relevant (in such a context) -Wdivision-by-zero warning:
int8_t h = a / 0u;
warning: division by zero is undefined [-Wdivision-by-zero]
Consider the following code:
#include <complex>
int main()
{
unsigned u = 1u;
auto result = static_cast<std::complex<int>>(u);
return 0;
}
Compiling with
g++ -std=c++11 -Werror -Wsign-conversion -o a.out source_file.cpp
Causes compile error
source_file.cpp: In function ‘int main()’:
source_file.cpp:8:51: error: conversion to ‘int’ from ‘unsigned int’ may change the sign of the result [-Werror=sign-conversion]
auto result = static_cast<std::complex<int>>(u);
^
clang reports a similar error
source_file.cpp:6:50: error: implicit conversion changes signedness: 'unsigned int' to 'const value_type' (aka 'const int') [-Werror,-Wsign-conversion]
auto result = static_cast<std::complex<int>>(u);
~~~~~~~~~~~ ^
The error does not make much sense at first sight, what am I missing?
You get a conversion warning not from the cast, but from inside construction of std::complex.
To 'fix' your example you should instead do:
auto result = std::complex<int>{static_cast<int>(u)};
In the following snippet no warnings are produced. g++4.4.3 -Wall -pedantic
//f is
void f(int );
f(3.14);
double d = 3.14;
int i = d+2;
I have a strong recollection of this being a warning, something along the lines of "Possible loss of precision". Was it removed or is my memory playing tricks on me?
How can i turn this into a warning in g++? I find this a useful warning, or is it a bad idea?
I can't even find anything appropriate at http://gcc.gnu.org/onlinedocs/gcc-4.4.5/gcc/Warning-Options.html
$ gcc -Wconversion test.c
test.c: In function ‘main’:
test.c:3: warning: conversion to ‘int’ from ‘double’ may alter its value
Use -Wconversion option. -Wall doesn't include it.
With -Wconversion option, GCC gives these warning messages:
warning: conversion to 'int' alters 'double' constant value
warning: conversion to 'int' from 'double' may alter its value
Apart from what other answers mention it is also worth mentioning that in C++0x {} initialization doesn't narrow. So instead of getting a warning you'll get an error for example
void f(int x)
{
// code
}
int main()
{
f({3.14}); // narrowing conversion of '3.14000000000000012434497875801753252744674682617e+0' from 'double' to 'int' inside { }
}
g++ 4.4 and above support initializer list (with -std=c++0x option)
In the following snippet no warnings are produced. g++4.4.3 -Wall -pedantic
//f is
void f(int );
f(3.14);
double d = 3.14;
int i = d+2;
I have a strong recollection of this being a warning, something along the lines of "Possible loss of precision". Was it removed or is my memory playing tricks on me?
How can i turn this into a warning in g++? I find this a useful warning, or is it a bad idea?
I can't even find anything appropriate at http://gcc.gnu.org/onlinedocs/gcc-4.4.5/gcc/Warning-Options.html
$ gcc -Wconversion test.c
test.c: In function ‘main’:
test.c:3: warning: conversion to ‘int’ from ‘double’ may alter its value
Use -Wconversion option. -Wall doesn't include it.
With -Wconversion option, GCC gives these warning messages:
warning: conversion to 'int' alters 'double' constant value
warning: conversion to 'int' from 'double' may alter its value
Apart from what other answers mention it is also worth mentioning that in C++0x {} initialization doesn't narrow. So instead of getting a warning you'll get an error for example
void f(int x)
{
// code
}
int main()
{
f({3.14}); // narrowing conversion of '3.14000000000000012434497875801753252744674682617e+0' from 'double' to 'int' inside { }
}
g++ 4.4 and above support initializer list (with -std=c++0x option)
In the following snippet no warnings are produced. g++4.4.3 -Wall -pedantic
//f is
void f(int );
f(3.14);
double d = 3.14;
int i = d+2;
I have a strong recollection of this being a warning, something along the lines of "Possible loss of precision". Was it removed or is my memory playing tricks on me?
How can i turn this into a warning in g++? I find this a useful warning, or is it a bad idea?
I can't even find anything appropriate at http://gcc.gnu.org/onlinedocs/gcc-4.4.5/gcc/Warning-Options.html
$ gcc -Wconversion test.c
test.c: In function ‘main’:
test.c:3: warning: conversion to ‘int’ from ‘double’ may alter its value
Use -Wconversion option. -Wall doesn't include it.
With -Wconversion option, GCC gives these warning messages:
warning: conversion to 'int' alters 'double' constant value
warning: conversion to 'int' from 'double' may alter its value
Apart from what other answers mention it is also worth mentioning that in C++0x {} initialization doesn't narrow. So instead of getting a warning you'll get an error for example
void f(int x)
{
// code
}
int main()
{
f({3.14}); // narrowing conversion of '3.14000000000000012434497875801753252744674682617e+0' from 'double' to 'int' inside { }
}
g++ 4.4 and above support initializer list (with -std=c++0x option)