Using GCC 4.8.*, when the warning -Wfloat-equal
is activated, the compiler warns about strict comparisons between floting point number, like in the following example:
double x = 3.14159;
double y = 1.11111;
if(x == y) // <-- this induces a warning
{ /* ... */ }
Now, imagine I want a class containing double variables and defining the equality operator:
class Complex // (it's only an example)
{
private:
double re;
double im;
public:
bool operator == (Complex const& z) const;
};
bool Complex::operator == (Complex const& z) const
{
return (this->re == z.re) && (this->im == z.im);
}
This does exactly what I expect. Of course, it induces a warning when I compile the class. In order to avoid it (because I understand the warning, thanks to the compiler, but I want to do that, and I don't want to continue to see the warning), I inform the compiler by this way:
#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wfloat-equal"
bool Complex::operator == (Complex const& z) const
{
return (this->re == z.re) && (this->im == z.im);
}
#pragma GCC diagnostic pop
Okay, I don't have the warning when I compile my Complex.cpp
file. But it's still dangerous to use the operator ==
on complex numbers, exaclty like it's dangerous to use operator ==
on double numbers (it's the reason for the existence of the option -Wfloat-equal
). Then, my question:
Is it possible to have a GCC warning (activated by -Wfloat-equal
) where the operator ==
on complex numbers is used ? It's not the existence of the operator I want to warn, but the usage.
Note: I defined the symmetric operator as a class member, but I'm open to have a class function bool equals(...) const
called by a bool operator == (Complex const&,Complex const&)
if it can simplify my expected behavior.
Note: I don't use C++11 for compatibility reasons.
This seems to work (live on gcc4.8.5):
__attribute__((warning ("your message")))
bool operator == (Complex const& z) const;
of course, you'll need to make sure the offending statement doesn't get optimized out ..
As it is, you'll need to dis/enable it manually (via some define) ... I don't know if gcc allows inspecting if a warning has been enabled or not.