I have implemented a simple BooleanVariable class, where I get the boolean value from template parameter V, and it's comparison function operator==:
#include <iostream>
template<bool V>
struct BooleanVariable {
BooleanVariable() = default;
constexpr bool operator()() const {
return V;
}
};
template<bool V>
bool operator==(const BooleanVariable<V> &, const BooleanVariable<V> &) {
return true;
}
template<bool V1, bool V2>
bool operator==(const BooleanVariable<V1> &, const BooleanVariable<V2> &) {
return false;
}
int main() {
std::cout<<(BooleanVariable<true>() == BooleanVariable<false>())<<"\n";
std::cout<<(BooleanVariable<false>() == BooleanVariable<true>())<<"\n";
std::cout<<(BooleanVariable<true>() == BooleanVariable<true>())<<"\n";
std::cout<<(BooleanVariable<false>() == BooleanVariable<false>())<<"\n";
return 0;
}
operator== works perfectly, that I wonder why these 2 comparison functions don't have ambiguity problem for the case when V1 == V2, as both of fit to the template. Why is
template<bool V1, bool V2>
bool operator==(const BooleanVariable<V1> &, const BooleanVariable<V2> &) {
return false;
}
not triggered when V1 == V2? Can someone explains to me?