我正在尝试理解为什么这段代码没有按预期工作。
在所有情况下,“default”都会被打印。对于最后一种情况,我期望第二个函数被调用。
#include <cstdio>
#include <vector>
#include <type_traits>
using namespace std;
struct Foo {
};
template<typename T, typename = void>
void compare(const T&a, const T&b) {
cout << "default" << endl;
}
template<typename T, std::enable_if_t<std::is_same<T, Foo>::value>>
void compare(const T& a, const T &b) {
cout << "In object" << endl;
}
int main(int argc, char const *argv[]) {
compare(1, 2);
{
vector<int> a, b;
compare(a, b);
}
{
Foo a, b;
compare(a, b);
}
return 0;
}
在所有情况下,“default”都会被打印。对于最后一种情况,我期望第二个函数被调用。
compare
函数中将参数(l, r)
切换为(a, b)
。 - Daniel H