Search code examples
c++templatesfriend

C++ friend comparison between two instantiations of one class template


I am trying to write a class template that provides a comparison operator between two instatiations with different template types. As is often the case, this operator is a non-member friend. A simplified example of what I am trying to achieve can be seen below.

template<typename T>
class Wrapper {
  // Base type, which must have a val() method to allow comparison
  T t_;
public:
  Wrapper(const T& t) : t_(t) {}

  // Comparison operator
  template<typename B>
  friend
  bool
  operator==(const Wrapper &a, const Wrapper<B>&b) {
    return a.t_.val()==b.t_.val();
  }
};

// First example type for Wrapper
class X {
  int x_;    
public:
  X(int x) : x_(x) {}

  int
  val() const {return x_;}
};

// Second example type for Wrapper
class Y {
  int y_;
public:
  Y(int y) : y_(y) {}

  int
  val() const {return 2*y_;}
};

int
main() {
  Wrapper<X> WX(X(4));
  Wrapper<Y> WY(Y(2));
  return WX==WY ? 1 : 0;
}

This example (g++ 4.4.0) doesn't compile: instead it complains that y_ from Wrapper<Y> is private and so inaccessible to the friend function, and I can see why. But how can I fix this? Adding friendship to the reversed function

  template<typename B>
  friend bool operator==(const Wrapper<B> &, const Wrapper &);

into the Wrapper class template body merely causes ambiguity to the compiler. I don't want to allow different instatiations of the Wrapper class to have access to the private members of each other - I want to restrict access to this one operator. Is this possible?

My laptop's in peril from being defenestrated, so any ideas would be appeciated by both me and the laptop (and the window for that matter).


Solution

  • Number of things:

    • When your class template is instantiated once for X and once for Y, you have two definitions of the operator==.

    • Move the operator== out of the class declaration.

    • Note that a friend is not a member. Hence the access violation related diagnostic.

    Try this:

    template<typename T>
    class Wrapper {
      // Base type, which must have a val() method to allow comparison
      T t_;
    public:
      Wrapper(const T& t) : t_(t) {}
    
      // Comparison operator
    
      template<typename A, typename B>
      friend bool
      operator==(const Wrapper<A> &a, const Wrapper<B>&b);
    };
    
    template<typename A, typename B>
      bool
      operator==(const Wrapper<A> &a, const Wrapper<B>&b) {
        return a.t_.val()==b.t_.val();
      }
    
    // First example type for Wrapper
    class X {
      int x_;    
    public:
      X(int x) : x_(x) {}
    
      int
      val() const {return x_;}
    };
    
    // Second example type for Wrapper
    class Y {
      int y_;
    public:
      Y(int y) : y_(y) {}
    
      int
      val() const {return 2*y_;}
    };
    
    int
    main() {
      Wrapper<X> WX(X(4));
      Wrapper<Y> WY(Y(2));
      return ::operator==(WX, WY) ? 1 : 0;
    }
    

    Though I still don't like the possibility that there are two possible friend operator== lurking in there...