I recently read Lao Zhao's blog post and realized that Lambda not only provides a syntax sugar for imitation functions, but also encapsulates environment variables to Implement Asynchronous function calls more easily.
Previously, asynchronous function calls were implemented using templates. Because c ++ before vs2008 does not support variadic templates and lambda, the implementation is to use macro-implemented 0 to N parameters of the imitation function. therefore, this modification is mainly based on retaining the previous Code and adding Lambda support. however, it takes a while to get the lambda return value, Google it, and then find the link:
Http://blog.csdn.net/zwvista/article/details/5531829
To facilitate the description, extract the following content:
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 onetypedef R result_type; }; template <typename R> struct function_traits<R (*)()> { // match ftypedef R result_type; }; template <typename F>typename function_traits<F>::result_typefoo(F f){return f();}int main(void){foo(f);foo(X());foo([](){ return 3; });}
After testing, the method mentioned here does not work because
& T: Operator () is not a static function of T, resulting in compilation failure
Considering that sizeof (function (x) does not actually call a function, I don't think decltype (function (x) actually calls the function.
& T: Operator ()
Modify
Decltype (T *) 0)-> operator ()())
This is successfully compiled in vs2010.
The complete code is as follows:
template<typename LAMBDA>struct function_traits{typedef decltype(((LAMBDA*)0)->operator()()) return_type;};template<typename RET>struct function_traits<RET(*)()>{typedef RET return_type; };template<typename RET,typename CLASS>struct function_traits<RET(CLASS::*)() const>{typedef RET return_type; };