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).