Compilation of 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; }
results in an ambiguous call error, because doAction is in the parent classes TestClass<1> and TestClass<2> .
main.cpp: 33: 15: Member 'doAction' found in several base classes of different types
But std::enable_if will not disable this ambiguity?
EDIT:
I think the real reason for this ambiguity is the same as in this question:
Why are multi-inherited functions with the same name, but different signatures cannot be considered overloaded functions?
Uncertainty 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 this was @skypjack's answer, but I still allowed its alternate method.