How do I name a lambda (anonymous) function?

Source: Internet
Author: User

C++11 adds the lambda (anonymous) function, which in practice finds it necessary to name anonymous functions (enhanced code readability).

This article describes an elegant way to implement the naming of anonymous functions.

I've found that any way to enhance code readability without violating compiler rules is to define an empty definition, such as:

/** * Macros within the MFC framework that identify message handlers have no practical meaning, but can be seen at a glance as a message processing function. */#ifndef afx_msg #define AFX_MSG//Intentional placeholder #endif

/**

* In and out macros are used to identify the direction in which a function parameter is passed in, some languages (C #)

* directly supports in and out keywords for enhanced readability.

*/

#ifndef in
#define In
#endif

#ifndef out
#define Out
#endif

Along this line of thought it is easy to derive a method named for anonymous functions, defining a meaningful empty macro, as follows:

/**
* Define the name of the anonymous function
*/
#define $ (NAME)

This symbol is selected because it is concise enough to not exclude the effects of some JavaScript frameworks. Your anonymous function can then be used in this way.

if ([&]$ (Testifdigitalzoom) ()->bool{
if (type = = Recalcrect_fitfull | | type = = recalcrect_fitbest)
{
return true;
}

if (Izoomzt > | | izoomzt < 0)
{
return false;
}

/* Scaled after change, whether the machine is scaled */
if (Fzoom * rectcalcor::_zoomkey <= 1.0f && fzoom >= imgsize.width * 1.0f/capsize.width * rectcalcor::_zoo Mkey)
{
return true;
}
Else
{
return false;
}
}())
{
}

What, isn't it cool?


How do I name a lambda (anonymous) function?

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.