Visual Studio 11 Development Guide (17) C ++ 11 Update-lambda expressions

Source: Internet
Author: User

Visual Studio may use lambda expressions (Forms of anonymous methods) in C ++ code since 2010 and more specifically in STL ). For example, if you look at the following code execution, when using these types of algorithms for_each, parallel_for, parallel_for_each, and so on.

1.std::deque<int> d1;2.    d1.push_back (2);3.    d1.push_back (1);4.    d1.push_back(3);5.    d1.push_back(0);6.    auto a=d1.begin ();7.    auto b=d1.end ();8.    std::sort(a,b);9.    10.    std::for_each (a,b,[](int i)11.    {12.            std::cout << i << std::endl;13.    });

 

Lambda starts to use two characters [] here to indicate that our capture syntax is different from that without local variables [] or [=] or that we capture all local variables by reference or backup respectively. Lambda does not capture any variables.

Now, this type of Lambda is implicitly converted to a function pointer. In other words, the old Win32 API is successfully called.
The example here is similar to createthreadpoolworkapi, pointing to the pointer parameter 1 of the function, and typing ptp_work_callback, which is obviously better than the original lambda.

 

1.PTP_POOL pool=CreateThreadpool(NULL);2.    TP_CALLBACK_ENVIRON cbEnviron;3.    InitializeThreadpoolEnvironment(&cbEnviron);4.    SetThreadpoolThreadMaximum (pool,4);    5.    BOOL bRet=SetThreadpoolThreadMinimum (pool,2);6.    7.    8.    PTP_WORK work=CreateThreadpoolWork([]( PTP_CALLBACK_INSTANCE Instance,PVOID Context,PTP_WORK Work)9.    {10.        11.        wprintf(L"Fait du boulot\n");12.    },NULL,&cbEnviron);13.    SubmitThreadpoolWork(work);    14.    WaitForThreadpoolWorkCallbacks(work,FALSE);15.    CloseThreadpoolWork(work);16.    CloseThreadpool(pool);

 

Another example API enumwindows, or we can use the old API to call the "Modern C ++ code" together

 

1.BOOL ret=EnumWindows ([](HWND hwnd,LPARAM lParam)->BOOL2.    {3.        const size_t MAX_SIZE=2048;4.        LPWSTR title=static_cast<LPWSTR>(_malloca(MAX_SIZE));        5.        if (title!=nullptr)6.        {7.            ZeroMemory (title,MAX_SIZE);8.            if (GetWindowTextLength (hwnd) >0)9.            {10.                GetWindowTextW  (hwnd,title,MAX_SIZE);11.                wprintf(L"%ls\n",title);12.                _freea(title);13.            }14.        }15.    16.        return TRUE;17.    },0);    

 

Download vs11 now

Http://www.microsoft.com/click/services/Redirect2.ashx? Cr_cc = 200098144

Related Article

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.