How to make GCC warn about using a class function?

Using GCC 4.8. * when the warning is -Wfloat-equalactivated, the compiler warns of strict comparisons between the flotation number, as in the following example:

double x = 3.14159;
double y = 1.11111;
if(x == y) // <-- this induces a warning
{ /* ... */ }

Now imagine that I need a class that contains double variables and defines an equality operator:

class Complex // (it 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 raises a warning when compiling the class. To avoid this (because I understand the warning, thanks to the compiler, but I want to do this, and I don't want to continue to see the warning), I tell the compiler as follows:

#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

, Complex.cpp. - == , exaccty, == ( -Wfloat-equal). , :

GCC ( -Wfloat-equal), == ? , , .

. , bool equals(...) const, bool operator == (Complex const&,Complex const&), .

. ++ 11 .

+4
2

, (live on gcc4.8.5):

__attribute__((warning ("your message")))
bool operator == (Complex const& z) const;

, , .

, / ( )... , gcc , .

+2

Exactly, ?

template<typename T>
struct Exactly : T
{
    explicit Exactly(T const& value) : T(value) {}
};

class Complex
{
    double re;
    double im;

public:
    Complex(double re, double im) : re(re), im(im) {}

    // Non-members require either friend access or public getters.
    double real() const { return re; }
    double imag() const { return im; }
};

#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wfloat-equal"
bool operator == (Complex const& a, Exactly<Complex> const& b)
{
    return (a.real() == b.real()) && (a.imag() == b.imag());
}
// two more overloads for completeness
bool operator == (Exactly<Complex> const& a, Complex const& b)
{
    return (a.real() == b.real()) && (a.imag() == b.imag());
}
bool operator == (Exactly<Complex> const& a, Exactly<Complex> const& b)
{
    return (a.real() == b.real()) && (a.imag() == b.imag());
}
#pragma GCC diagnostic pop

(, ):

Exactly<Complex> exactlyOne(Complex(1.0, 0.0));

"maker", , Complex:

template<typename T>
Exactly<T> exactly(T const& value)
{
    return Exactly<T>(value);
}

operator != .

. operator == Exactly, , operator ==(Complex const& a, Complex const& b) , . .

0

Source: https://habr.com/ru/post/1689443/


All Articles