c++でラムダ関数のパラメータの型にアクセスするにはどうすればよいですか? 以下は機能しません。
template <class T> struct capture_lambda {
};
template <class R, class T> struct capture_lambda<R(T)> {
static void exec() {
}
};
template <class T> void test(T t) {
capture_lambda<T>::exec();
}
int main() {
test([](int i)->int{ return 0; });
}
コンパイラは特殊化ではなくテンプレート プロトタイプを選択するため、上記はコンパイルされません。
上記を行う方法はありますか?
私が実際に達成しようとしているのは次のとおりです。関数のリストがあり、呼び出す適切な関数を選択したいと考えています。例:
template <class T, class ...F> void exec(T t, F... f...) {
//select the appropriate function from 'F' to invoke, based on match with T.
}
たとえば、「int」を取る関数を呼び出したいとします。
exec(1, [](char c){ printf("Error"); }, [](int i){ printf("Ok"); });