How can I access lambda types in C ++ 0x?

How can I access parameter types of a lambda function in C ++? The following does not work:

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; });
}

The above does not compile because the compiler chooses a template prototype instead of specialization.

Is there any way to do this?

What I'm actually trying to achieve is this: I have a list of functions, and I want to select the appropriate function to call. Example:

template <class T, class ...F> void exec(T t, F... f...) {
    //select the appropriate function from 'F' to invoke, based on match with T.
}

For example, I want to call a function that takes an 'int':

exec(1, [](char c){ printf("Error"); }, [](int i){ printf("Ok"); });
+5
source share
1 answer

, - - , . , , .

, operator() s.

+2

All Articles