Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to use __stdcall to qualify C++ lambda?

Foreword--I love C++ lambda, if possible I will use it everywhere.

Now I have a lambda requirement, I need to a __stdcall lambda. But I get the following error message:

 error C2664: 'EnumWindows' : cannot convert parameter 1 from '`anonymous-namespace'::<lambda1>' to 'WNDENUMPROC'
1>          No user-defined-conversion operator available that can perform this conversion, or the operator cannot be called

Anybody can help me? Here is my code(EnumWindowsProc is in the function scope):

        auto EnumWindowsProc = 
            [&](HWND hwnd, LPARAM lParam) mutable -> bool
        {
            return true;
        };
        EnumWindows(EnumWindowsProc, NULL);
like image 828
Triumphant Avatar asked Jul 18 '13 03:07

Triumphant


1 Answers

I just noticed you have the visual studio 2010 tag. Stateless lambdas were implemented in VC11. Reference:

After lambdas were voted into the Working Paper (v0.9) and mutable lambdas were added (v1.0), the Standardization Committee overhauled the wording, producing lambdas v1.1. This happened too late for us to implement in VC10, but we've already implemented it in VC11. The lambdas v1.1 wording clarifies what should happen in corner cases like referring to static members, or nested lambdas. This fixes a bunch of bugs triggered by complicated lambdas. Additionally, stateless lambdas are now convertible to function pointers in VC11. This isn't in N2927's wording, but I count it as part of lambdas v1.1 anyways. It's FDIS 5.1.2 [expr.prim.lambda]/6: "The closure type for a lambda-expression with no lambda-capture has a public non-virtual non-explicit const conversion function to pointer to function having the same parameter and return types as the closure type’s function call operator. The value returned by this conversion function shall be the address of a function that, when invoked, has the same effect as invoking the closure type’s function call operator." (It's even better than that, since we've made stateless lambdas convertible to function pointers with arbitrary calling conventions. This is important when dealing with APIs that expect __stdcall function pointers and so forth.)

Also, note that this conversion happens when there is no capture specification as mentioned in the second bolded quote.

like image 162
Jesse Good Avatar answered Oct 18 '22 02:10

Jesse Good