Stateless Lambdas and the Old Guard

Forget for a moment that Windows 8 was just released. A few rogue programmers at Microsoft snuck in a new release of the C++ compiler that was coincidentally completed at the same time. Microsoft doesn’t really want you to know about this release. They would rather you just program in C# so that they can keep all the best programming goodies to themselves. No one will ever confess this publicly but there are only two languages of significance on Windows today and they are C++ and JavaScript. I know, quite an odd couple.

Enough gossip. What I want to share with you today is a small change that was made to lambdas in the C++ compiler to allow them to be implicitly converted to function pointers. I can just hear you say, who cares?! Well again, if you forget for a moment that Windows 8 was also released you may recall this vast library called the Windows API that, by the way, Windows 8 was written in and you may also recall that a great many of these libraries were extensible by means of function pointers.

What this means is that you can now use lambdas to write these callbacks rather than having to define them elsewhere. This can introduce significant savings in complexity. Consider the TrySubmitThreadpoolCallback function from the Windows thread pool. Prior to Visual C++ 2012 if you wanted to allow a lambda to be submitted to the thread pool you would have to first create some concurrent queue and then have at least two functions. The first would queue the lambda-turned-function object before calling TrySubmitThreadpoolCallback. The second function being the actual callback function that would dequeue the function object from the concurrent queue and call it. That represents quite a lot of coding and run-time overhead just for the convenience of using lambdas.

Now thanks to the Visual C++ 2012 compiler you can use lambdas with the Windows API with no overhead whatsoever. No concurrent queue. No function objects. Just the thread pool.

TrySubmitThreadpoolCallback([](PTP_CALLBACK_INSTANCE, void *)
{
    printf("hello from the thread pool\n");
},
nullptr, nullptr);

Clearly this only works for stateless lambdas as they are binary-compatible with function pointers whereas lambdas that capture variables are not. Still this represents a nice improvement for the C++ programmer on Windows. Enjoy!

4 thoughts on “Stateless Lambdas and the Old Guard

  1. James McNellis

    Note that this feature isn’t Visual C++-specific; it’s part of the C++11 language specification, so at some point all modern C++ compilers should support this. (This feature was added to the language very late, in early 2010 if I recall correctly, which is likely why it wasn’t available in Visual C++ 2010).

    As an added bonus in Visual C++: the function pointer can have any calling convention (e.g., cdecl, stdcall, fastcall).

    Reply
  2. fourbadcats

    This is so sweet! Not a week ago I fought with VS2010 trying to convince VC to do this very thing.

    Let’s dump all the windows in the system….

    EnumWindows([](HWND hwnd,LPARAM lp)->BOOL{
    TCHAR name[300] = {0};
    if (GetWindowText(hwnd,name,300))
    wcout << hwnd << ": " << wstring(name) << endl;
    return TRUE;
    },
    NULL);

    Thanks for the notice Kenny!

    Reply

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s