Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

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

like image 999
Captain Giraffe Avatar asked Apr 05 '11 14:04

Captain Giraffe


People also ask

What does Reinterpret_cast mean?

reinterpret_cast is a type of casting operator used in C++. It is used to convert a pointer of some data type into a pointer of another data type, even if the data types before and after conversion are different. It does not check if the pointer type and data pointed by the pointer is same or not.

Is Reinterpret_cast safe?

The result of a reinterpret_cast cannot safely be used for anything other than being cast back to its original type. Other uses are, at best, nonportable. The reinterpret_cast operator cannot cast away the const , volatile , or __unaligned attributes.

What is static_cast int in C++?

The static_cast operator converts variable j to type float . This allows the compiler to generate a division with an answer of type float . All static_cast operators resolve at compile time and do not remove any const or volatile modifiers.

How do you cast in C++?

Typecasting is making a variable of one type, such as an int, act like another type, a char, for one single operation. To typecast something, simply put the type of variable you want the actual variable to act as inside parentheses in front of the actual variable. (char)a will make 'a' function as a char.


2 Answers

$ gcc -Wconversion test.c  test.c: In function ‘main’: test.c:3: warning: conversion to ‘int’ from ‘double’ may alter its value 
like image 154
NPE Avatar answered Oct 01 '22 15:10

NPE


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

like image 32
Nawaz Avatar answered Oct 01 '22 17:10

Nawaz