Home > database >  incorrect function call on overload operator
incorrect function call on overload operator

Time:07-08

In the code, why is (10 != i) calling == instead of !=? The other two call !=

#include <iostream>

class Integer
{
  int x;

public:
  bool
  operator== (const Integer &i)
  {
    std::cout << "==";
    return x == i.x;
  }
  bool
  operator!= (const Integer &i)
  {
    std::cout << "!=";
    return x != i.x;
  }
  Integer (int t = 0) { x = t; }
};

int
main ()
{
  Integer i;
  std::cout << (i != i) << '\n';   // calls !=
  std::cout << (i != 100) << '\n'; // calls !=
  std::cout << (10 != i) << '\n';  // calls ==
}

CodePudding user response:

Prior to C 20, you'd need to add two free functions for the comparison where the int is on the left-hand side:

bool operator==(int lhs, const Integer& rhs) {
    return rhs == lhs;
}

bool operator!=(int lhs, const Integer& rhs) {
    return rhs != lhs;
}

You should also make the member comparison operators const qualified:

class Integer {
public:
    //...
    bool operator==(const Integer &i) const { // note const
        std::cout << "==";
        return x == i.x;
    }
    bool operator!=(const Integer &i) const { // note const
        std::cout << "!=";
        return x != i.x;
    }
    //...
};

You could also remove the member operators to simplify things. Now the left-hand side int will be implicitly converted to Integer and then compared with the right-hand side:

class Integer {
    int x;

public:
    Integer(int t = 0) : x{t} {}

    friend bool operator==(const Integer& lhs, const Integer& rhs) {
        return rhs.x == lhs.x;
    }

    friend bool operator!=(const Integer& lhs, const Integer& rhs) {
        return !(rhs == lhs);
    }    
};

Since you've tagged this C 20, you can let operator== do all the work. See Default comparisons. It'll be used for operator!= too.

class Integer {
    int x;

public:
    bool operator==(const Integer &i) const {
        std::cout << "==";
        return x == i.x;
    }
    
    Integer(int t = 0) : x{t} {}
};

... and it'll correctly show that it used operator== for all your != comparisons (and negated it).

More from Defaulted equality comparison:

A class can define operator== as defaulted, with a return value of bool. This will generate an equality comparison of each base class and member subobject, in their declaration order. Two objects are equal if the values of their base classes and members are equal. The test will short-circuit if an inequality is found in members or base classes earlier in declaration order.

Per the rules for operator==, this will also allow inequality testing

This means that you will in fact get away with the below only since C 20:

class Integer {
    int x;

public:
    bool operator==(const Integer &i) const = default;
    
    Integer(int t = 0) : x{t} {}
};

or even better, get all the comparison operators for free by defaulting the spaceship operator <=>:

class Integer {
    int x;

public:
    auto operator<=>(const Integer &i) const = default;
    
    Integer(int t = 0) : x{t} {}
};

CodePudding user response:

There are two new additions to C 20 that made this possible (note that your code doesn't compile in earlier standard versions).

  1. Compiler will attempt to replace a != b with !(a == b) if there is no suitable != for these arguments.

  2. If there is no suitable a == b, compiler will attempt b == a as well.

So, what happens - compiler first notices that it doesn't know how to compare 10 != i, so it tries !(10 == i). There is still no suitable comparison, so it tries !(i == 10) and it can finally be done using your implicit constructor to convert 10 to Integer.
It can be easily verified by adding more info to debug print:

  bool
  operator== (const Integer &i) const
  {
    std::cout << x << "==" << i.x << ' ';
    return x == i.x;
  }

will print 0==10 1 in the last line (see it online).


As noticed in comments, you don't even need operator !=, due to aforementioned behaviour C 20 compiler will automatically convert any such call to operator ==.

  • Related