有没有一种方法,使用SFINAE来检测一个给定的类是否有一个自由函数重载?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
是在一个和用户提供自己的类型)。
我不能使用C++ 0x,我需要一个适用于所有现代编译器的工作解决方案。
注意:解决方案similar question不幸的是不能在这里工作。
非常干净的问题。我也想知道解决方案。 – 2011-01-26 14:50:57
是否要检测仅在实例化处可见的函数? – 2011-02-05 10:50:49