I have several functions that I would like to work for derived classes of a CRTP base class. The issue is that if I pass the derived classes into the free functions meant for the CRTP class, ambiguities arise. A minimal example to illustrate this is this code:
template<typename T>
struct A{};
struct C : public A<C>{};
struct B{};
template<typename T, typename U>
void fn(const A<T>& a, const A<U>& b)
{
std::cout << "LT, RT\n";
}
template<typename T, typename U>
void fn(const T a, const A<U>& b)
{
std::cout << "L, RT\n";
}
template<typename T, typename U>
void fn(const A<T>& a, const U& b)
{
std::cout << "LT, R\n";
}
int main()
{
C a; // if we change C to A<C> everything works fine
B b;
fn(a,a); // fails to compile due to ambiguous call
fn(b,a);
fn(a,b);
return 0;
}
Ideally I would like this to work for the derived classes as it would if I were to use the base class (without having to redefine everything for the base classes, the whole point of the CRTP idiom was to not have to define fn for multiple classes).