gcc 4.8 failed to check type conversion in c++ template

135 Views Asked by At

I'm trying to update a c++ project's compiler from 4.8.4 to 7.5.0. The codes can be well compiled with g++4.8.4. However, when I try to compile it with newer g++ 7.5.0. Lots of Werror=conversion errors are triggered.

Here is the demo code abstracted from my project:

// main.cc
template <typename T>
int Hello(T obj) {
  float arg = 1.0;
  // return obj.func(arg);    // good, error detected for both g++ 4.8.4 and 7.5.0
  return obj.func(arg) * 2;   // failed to detect conversion error for 4.8.4, success for 7.5.0
}

class A {
  public:
    int func(int a) {
      return a;
    }
};

int main() {
  A a;
  Hello(a);
  return 0;
}

build:

g++ main.cc -Werror=conversion

As commented above, in a c++ template function, g++4.8 failed to detect the type conversion error when it's in a expression. But it will sucess if it is alone. I'm very curious about this.
My Question:
Is this a bug of g++4.8? Why does expression matter?

0

There are 0 best solutions below