9

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);
Triumphant
  • 948
  • 1
  • 11
  • 28

1 Answers1

11

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.

Jesse Good
  • 50,901
  • 14
  • 124
  • 166
  • Can I have the lambda function in the function scope to have __stdcall qualifier? – Triumphant Jul 18 '13 at 03:53
  • 1
    Interesting, I never knew they could be `__stdcall` when needed. – chris Jul 18 '13 at 03:58
  • 1
    @Triumphant: Try casting it `static_cast(EnumWindowsProc)`, I don't have a compiler to test right now though. **EDIT** Nevermind, this probably won't work in VC10 because the conversion to function pointer is not implemented. – Jesse Good Jul 18 '13 at 04:00
  • 1
    @Triumphant I don't think there's a way to get this to work with VC10. As Jesse's answer explains, implicit conversion of stateless lambdas to function pointers is only available from VC11 onwards. If you upgrade to VC11, a `static_cast` with the appropriate calling convention [should fix the problem](http://stackoverflow.com/a/14169489/241631). – Praetorian Jul 18 '13 at 04:05