为什么这会“按预期进行”?我的理解是,这不起作用:
template <class T, class U>
auto x(T a, U b) -> decltype(a<b ? a:b) {
return a > b ? a : b;
}
int main() {
cout << x<long, double>(1, 2.01) << endl;
cout << x<long, double>(5, 2.01) << endl;
}
我尝试了其他一些组合,例如:
template <class T, class U>
auto x(T a, U b) -> decltype(a<b ? a:a) {
return a > b ? a : b;
}
这样,它不会编译并显示错误,实际上第二个组合失败并显示>]
compile time error: Error C2440 'return': cannot convert from 'U' to 'T &'
这是预期的。我的理解是,第一个函数也应该会因相同的错误而失败,尽管它可以正常工作。
为什么这会“按预期进行”?我的理解是,这不应该起作用:template