我们可以选择在这种情况下应该使用哪个函数模板重载吗?
struct X { };
struct A { A(X){} };
struct B { B(X){} };
template<class T>
void fun(T, A) { }
template<class T>
void fun(T, B) { }
int main() {
/* explicitly choose overload */ fun(1, X());
}
错误:
error: call of overloaded 'fun(int, X)' is ambiguous
/* explicitly choose overload */ fun(1, X());
^
candidate: void fun(T, A) [with T = int]
void fun(T, A) { }
^~~
candidate: void fun(T, B) [with T = int]
void fun(T, B) { }
^~~
对于正常的功能,它看起来像这样:
void fun(A){}
void fun(B){}
int main() {
((void(*)(A))(fun))(X());
}
这可能吗?
最佳答案
改进你的例子,你可以试试
((void(*)(int, A))(fun))(1, X());
关于c++ - 明确选择应该使用哪个模板重载,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43166804/