有沒有一種方法,使用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