Compiling this code :
#include <iostream>
template <int N>
struct TestClass {
template <int N2, typename std::enable_if<N2 == N, int>::type = 0>
void doAction() { std::cout << "TestClass::doAction<" << N << ">();\n"; }
};
struct HostClass : public TestClass<1>, public TestClass<2> {
};
int main(int argc, const char * argv[]) {
HostClass hostClass;
hostClass.doAction<1>();
hostClass.doAction<2>();
return 0;
}
leads to an ambiguous call error because doAction
is both in TestClass<1>
and TestClass<2>
parent classes.
main.cpp:33:15: Member 'doAction' found in multiple base classes of different types
But std::enable_if
would not disable this ambiguity ?
EDIT:
I think the real reason to this ambiguity is the same than in this question :
The ambiguity can be resolved as shown in the answer with the using
keyword :
#include <iostream>
template <int N>
struct TestClass {
template <int N2, typename std::enable_if<N2 == N, int>::type = 0>
void doAction() { std::cout << "TestClass::doAction<" << N << ">();\n"; }
};
struct HostClass : public TestClass<1>, public TestClass<2> {
using TestClass<1>::doAction;
using TestClass<2>::doAction;
};
int main(int argc, const char * argv[]) {
HostClass hostClass;
hostClass.doAction<1>(); // OK, compile
hostClass.doAction<2>(); // OK, compile
//hostClass.doAction<3>(); // OK, doesn't compile : "candidate template ignored: disabled by 'enable_if' [with N2 = 3]"
return 0;
}
I don't know if it was what @skypjack answer meant but I let it anyway for its alternative method.