-Wfloat-equal, template and g++-4.3.3

Hi,

I'm using the following code:

template <typename T>
bool equals(T x, T y)
{
if (numeric_limits<T>::is_exact)
{
// Comparison for int's
return x == y;
}
else
{
// Comparison for doubles
... compare using numeric_limits<T>::epsilon() ...
}
}

Now when I compile that code g++ throws a warning of "unsafe comparison"
when I call that function for doubles (I have defined -Wfloat-equal).
The problem is that the warning is due to that first comparison. But why?
Isn't that numeric_limits<T>::is_exact a constant?
The line is never executed and should be optimized out. Could someone who's
expert in g++ tell how to use that kind of a function with -Wfloat-equal
enabled so that g++ won't give any "false" warnings? :)

If T == float or double the compiler sees x == y which is a comparison between floats and it gives you a warning. I wouldn't be executed but the compiler doesn't know
The right solution is to use template specialization to avoid the warning and the extra if() check.


Topic archived. No new replies allowed.