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
andbar
型があり、関数はそれらを認識していないため、単純なオーバーロードは機能しませんcall
(基本的call
に a 内にあり、ユーザーが独自の型を提供します)。
私は C++0x を使用できません。最新のすべてのコンパイラに対応するソリューションが必要です。
注: 残念ながら、同様の質問に対する解決策はここでは機能しません。