I want to write a proxy class, that takes a template value and can be compared to any class that the template can be compared to.
template <class T>
class Proxy {
public:
Proxy(T value) : _value(value) {}
template <class U> // this should exist only if the T == U operator is defined
bool operator==(U const& other) const { return _value == other; }
template <class U> // this should exist only if U == T is defined
friend bool operator==(U const& first, Proxy<T> const& second) const { return first == second._value; }
private:
T _value;
};
for example, since this is legal code:
bool compare(std::string first, std::string_view second) {
return first == second;
}
I want this to be legal, too:
bool compare(std::string first, Proxy<std::string_view> second) {
return first == second;
}
but just to clarify, this should work for any classes that can be compared, or can be implicitly converted in order to be compared. Can I define a template conditional that will check for either case?