有没有办法使用 SFINAE 来检测给定类的自由函数是否重载?
基本上,我有以下解决方案:
struct has_no_f { };
struct has_f { };
void f(has_f const& x) { }
template <typename T>
enable_if<has_function<T, f>::value, int>::type call(T const&) {
std::cout << "has f" << std::endl;
}
template <typename T>
disable_if<has_function<T, f>::value, int>::type call(T const&) {
std::cout << "has no f" << std::endl;
}
int main() {
call(has_no_f()); // "has no f"
call(has_f()); // "has f"
}
简单的重载call
是行不通的,因为实际上有很多foo
和bar
类型,并且call
函数不知道它们(基本上call
在 a 内部,用户提供自己的类型)。
我不能使用 C++0x,我需要一个适用于所有现代编译器的工作解决方案。
注意:不幸的是,类似问题的解决方案在这里不起作用。