C ++ 0x: how to obtain the return type of a Lambda expression

Source: Internet
Author: User

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


 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.