我声明了两个模板,第一个将参数x从type 转换T为type U,第二个从type转换U为type T.如果我cast用10 调用,编译器不会抱怨.我认为两者都符合要求使用,因此应该有歧义,这是真的吗?此代码打印10.
#include <iostream>
template<typename T, typename U>
U cast(T x) {
return static_cast<U>(x);
}
template<typename T, typename U>
T cast(U x) {
return static_cast<T>(x);
}
int main() {
std::cout << cast<int,float>(10) << '\n';
}
Run Code Online (Sandbox Code Playgroud) c++ templates overload-resolution template-argument-deduction