C++0x尝鲜:如何获取lambda表达式的返回类型

int f() { return 1; } struct X { float operator () () const { return 2; } // If the following is enabled, program fails to compile // mostly because of ambiguity reasons. //double operator () (float i, double d) { return d*f; } }; template <typename T> struct function_traits : function_traits<decltype(&T::operator())> // match X or lambda { // This generic template is instantiated on both the compilers as expected. }; template <typename R, typename C> struct function_traits<R (C::*)() const> { // inherits from this one typedef R result_type; }; template <typename R> struct function_traits<R (*)()> { // match f typedef R result_type; }; template <typename F> typename function_traits<F>::result_type foo(F f) { return f(); } int main(void) { foo(f); foo(X()); foo([](){ return 3; }); }

你可能感兴趣的:(C++,c,function,struct,lambda,float)