C++ range for loop, avoiding narrowing error [duplicate] - c++

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)

Related

Disable GCC narrowing conversion errors

I have code from over 20 years in C/C++ and one technique used to handle variable data sizes was to let automatic type conversion handle it.
For example:
#define MY_STATUS_UNDEFINED (-1)
Then if it was compared/used against a int64_t it was auto expanded to -1LL, for uint64_t to 0xFFFFFFFFFFFFFFFF, for uint32_t to 0xFFFFFFFF, int16_t to -1, uint16_t to 0xFFFF, etc..
However, now I'm getting errors with the newer gcc versions that complain about narrowing conversion. In this case it was a switch statement with a UINT variable.
switch (myuint) {
case MY_STATUS_UNDEFINED:
break;
}
What is the recommended way to get it to not error out?
What is the easiest way to get it to not error out?
What is the cleanest way so it doesn't error out and doesn't give any warning message?
Essentially, I want it to do the auto type conversion properly but no warning or errors.
Using the following brief example:
#include <cstdint>
#include <iostream>
#define MY_STATUS_UNDEFINED (-1)
void bar()
{
std::cout << "It works\n";
}
void foo(uint32_t n)
{
switch (n) {
case MY_STATUS_UNDEFINED:
bar();
break;
}
}
int main()
{
foo(0xFFFFFFFF);
return 0;
}
You are seeing the following error from gcc:
error: narrowing conversion of ‘-1’ from ‘int’ to ‘unsigned int’ [-Wnarrowing]
Now, pay careful attention to the error message. gcc is telling the exact option to shut this off. See the [-Wnarrowing] annotation? That's the compilation flag that's responsible for producing this error message.
To turn it off, stick a "no-" in front of it:
-Wno-narrowing
All gcc diagnostics use this convention. Now the shown code will compile, run, and produce the expected result:
It works!
Add -Wno-narrowing to your global compilation options.
You should really consider this to be only a temporary band-aid solution. These compilation errors are exactly what you want. They're telling you about real or potential problems. -Wall -Werror -Wsuggest-override -Wreturn-type are my favorite compilation options.

No warning of loss in precision when implicitly converting from double to int [duplicate]

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)

GCC allowing implicit int to pointer conversion?

So I mistakenly passed an int into a function parameter expecting a pointer to a const object and I got this warning from GCC:
jni/../../../Source/Controller/UserExperienceManager.cpp: In member function 'void CUserExperienceManager::SendUXPToPOS(Types::CString)':
jni/../../../Source/Controller/UserExperienceManager.cpp:243:78: warning: invalid conversion from 'int' to 'const WebService_POSAgent::CDataCheck*' [-fpermissive]
jni/../../../../../Core/WebServices/Services/POSAgent/POSAgent.h:80:18: warning: initializing argument 2 of 'CEvent& WebService_POSAgent::CPOSAgent::AddUserExperienceID(Types::CString, const WebService_POSAgent::CDataCheck*, Types::CString)' [-fpermissive]
I tried to create a reproducible sample of this but I wasn't able to and I can't share too much of the code here. I compiled the same thing on MSVC9 and it properly gave me an error. I'm just in shock that I'm not getting an error from GCC on this! Anyone know why? Here are some simple snippets:
Function Declaration (class member function):
CEvent& AddUserExperienceID(CString userExperienceId, CDataCheck const* check, CString requestId = REQUEST_ID);
Function Call Site:
int nCheckNum = /*some value*/;
CPOSAgent::Instance().AddUserExperienceID(m_UserExperienceId, nCheckNum);
You're compiling that code with -fpermissive set which is downgrading the error to a warning.
-fpermissive
Downgrade some diagnostics about nonconformant code from errors to warnings.
Thus, using -fpermissive will allow some nonconforming code to compile.
What does the fpermissive flag do?

Compiler does not give error when using list initialization which will cause information loss

In c++ primer(5th), it mentioned:
When used with variables of built-in type, this form of initialization
has one
important property: The compiler will not let us list initialize variables of built-in type if the initializer might lead to the loss
of information:
longdouble ld = 3.1415926536;
int a{ld}, b = {ld}; // error: narrowing conversion required
int c(ld), d = ld; // ok: but value will be truncate
I compile the code using gcc4.8.1 , it only give a warning rather than an error.
g++ -W -Wall -Wextra -pedantic -std=c++0x -o m main.cpp
main.cpp: In function ‘int main()’:
main.cpp:64:13: warning: narrowing conversion of ‘ld’ from ‘long double’ to ‘int’ inside { } [-Wnarrowing]
int a{ld}, b= {ld};
^
main.cpp:64:22: warning: narrowing conversion of ‘ld’ from ‘long double’ to ‘int’ inside { } [-Wnarrowing]
int a{ld}, b= {ld};
Is there any flags that will turn on the feature of the important property ?
A quick search for "gcc diagnostic flag" turns up documentation resources.
Inside your program, you could do this:
#ifdef __GNUC__
# pragma GCC diagnostic error "-Wnarrowing"
#endif
There is a command-line option too: -Werror=narrowing, but since you want to alter the semantic meaning of the program itself according to GCC, putting it in the source code is probably more appropriate.
Note, when it makes a difference other than simple well-formedness, such as in overload selection, GCC does diagnose the condition correctly.
The standard never calls for errors or for warnings: the standard only requires an implementation to issue a diagnostic. Whether such a diagnostic takes the form of a compiler error, or a warning, or something entirely different from them both, is outside the scope of the standard.

g++ -Wall not warning about double-> int cast

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)