conversion warning

  • Thread starter Alexandru Mosoi
  • Start date
A

Alexandru Mosoi

Hi

Recently I've made a stupid bug. Here is a snippet:

long long a, b;
....
int c = min(a, b);

But this bug could have been prevented if g++ would warn me about loss
of precision. Do you know any flag that can turn on this kind of
warning? I tried -Wconversion, but I believe it's working only on
float <-> double conversion.

Alexandru
 
V

Victor Bazarov

Alexandru said:
Recently I've made a stupid bug. Here is a snippet:

long long a, b;
...
int c = min(a, b);

Note that C++ does not have 'long long' type.
But this bug could have been prevented if g++ would warn me about loss
of precision. Do you know any flag that can turn on this kind of
warning? I tried -Wconversion, but I believe it's working only on
float <-> double conversion.

If you need an answer to a G++ specific question, consider posting
to the G++ specific newsgroup, please.

V
 
B

BobR

Alexandru Mosoi wrote in message...
Hi
Recently I've made a stupid bug. Here is a snippet:

long long a, b;
...
int c = min(a, b);

But this bug could have been prevented if g++ would warn me about loss
of precision. Do you know any flag that can turn on this kind of
warning? I tried -Wconversion, but I believe it's working only on
float <-> double conversion.
Alexandru

Why not just check it:

{
long long a, b;
if( min(a, b) > long long( std::numeric_limits<int>::max() ) ){
throw std::runtime_error(" result too large for type int.");
}
int c = min(a, b);
}

Or, truncate it:

int c = ( min(a, b) & 0x7fffffff ); // assuming sizeof int == 4

Check if it was truncated:

if( long long( c ) != min(a, b) ){
std::cout<<"Truncated"<<'\n';
}

For compiler questions, you need to ask on an NG for your compiler.
For GCC g++, gnu.g++.help
I think if you just turn on warnings (-Wall -W), it will tell you of
incorrect assignments. ( I've got a big project in progress, or I'd test
that out.)

Also:
-Wlong-long
Warn if long long type is used. This is default. To inhibit the warning
messages, use -Wno-long-long. Flags -Wlong-long and -Wno-long-long are taken
into account only when -pedantic flag is used.
 
A

Alexandru Mosoi

Alexandru Mosoi wrote in message...



Why not just check it:

{
long long a, b;> ...

if( min(a, b) > long long( std::numeric_limits<int>::max() ) ){
throw std::runtime_error(" result too large for type int.");
}
int c = min(a, b);

}

Or, truncate it:

int c = ( min(a, b) & 0x7fffffff ); // assuming sizeof int == 4

Check if it was truncated:

if( long long( c ) != min(a, b) ){
std::cout<<"Truncated"<<'\n';
}
None of you solution is good for me because I accidentally wrote _int
c_ instead of _long long c_. However I would have avoided the mistake
if compiler was a little bit clever. I've done the bug in a
programming contest where you don't really care about portability and
stuff like that. If it compiles than it perfect. However little
warnings are more than welcome.
For compiler questions, you need to ask on an NG for your compiler.
For GCC g++, gnu.g++.help
hmm... i've posted a message, i'm waiting for a reply.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,201
Messages
2,571,049
Members
47,654
Latest member
LannySinge

Latest Threads

Top