3

If I pass a double to a function requiring long, g++ warns of conversion problem, but if I pass a const double to a function requiring long, g++ is happy. The warning is the following:

warning: conversion to ‘long int’ from ‘double’ may alter its value [-Wconversion]

I would like g++ to give me a warning whether I pass a double or a const double. How would I do so?

I have makefile and some code you can run. I like to turn on as many warnings as I can, but perhaps one is implicitly shutting off another? I'm not sure.

Here's the Makefile:

WARNOPTS=-Wall -Wextra -pedantic \
      -Wdouble-promotion -Wformat=2 -Winit-self \
      -Wmissing-include-dirs -Wswitch-default -Wswitch-enum \
      -Wundef -Wunused-function -Wunused-parameter \
      -Wno-endif-labels -Wshadow \
      -Wpointer-arith \
      -Wcast-qual -Wcast-align \
      -Wconversion \
      -Wsign-conversion -Wlogical-op \
      -Wmissing-declarations -Wredundant-decls \
      -Wctor-dtor-privacy \
      -Wnarrowing -Wnoexcept -Wstrict-null-sentinel \
      -Woverloaded-virtual \
      -Wsign-compare -Wsign-promo -Weffc++


BUILD := develop
cxxflags.develop := -g $(WARNOPTS)
cxxflags.release := 
CXXFLAGS := ${cxxflags.${BUILD}}

foo: foo.cpp
    g++ $(CXXFLAGS) -o $@ $^

Here's foo.cpp:

// foo.cpp

#include <iostream>
#include <string>

using namespace std;

const double WAITTIME = 15;  // no warning on function call
//double WAITTIME = 15;  // warning on function call

bool funcl( long time);


bool funcl( long time ) {
  cout << "time = " << time << endl;
  return true;
}





int main() {
  string rmssg;

  funcl( WAITTIME );
  return 0;
}

This is the version of g++ that I am using:

g++ --version
g++ (Debian 4.7.2-5) 4.7.2

Thanks for the help!

Shafik Yaghmour
  • 154,301
  • 39
  • 440
  • 740
Bitdiot
  • 1,506
  • 2
  • 16
  • 30
  • 1
    Does it warn if you make the constant `15.5`? I'm guessing that when passing a `double` variable, as it doesn't know what the value is, it gives the warning. In your case, it "knows" that there won't be any truncation and doesn't complain. – TripeHound Jul 10 '14 at 13:19
  • 1
    The reasoning is probably pretty similar to [this case](http://stackoverflow.com/a/21273849/1708801) since `15` is a literal constant expression the compiler can make assumptions about whether the conversion will overflow or not. – Shafik Yaghmour Jul 10 '14 at 13:21
  • I see. You guys are right. If I change the const double with a literal value to 15.5, I get the complaint. I didn't think to try that because at the function call its just an ordinary double or const double, right? Anyways, thanks for helping me out. – Bitdiot Jul 10 '14 at 13:26

1 Answers1

4

This looks like a design decision by gcc if we look at the Wconversion wiki, it says:

[...]The option should not warn for explicit conversions or for cases where the value cannot in fact change despite the implicit conversion.

If we look at the assembly for this code we can see gcc is actually using a constant value of 15 instead of a variable which means it is also performing constant folding as well.

Shafik Yaghmour
  • 154,301
  • 39
  • 440
  • 740