简体   繁体   English

如何使用__stdcall来限定C ++ lambda?

[英]how to use __stdcall to qualify C++ lambda?

Foreword--I love C++ lambda, if possible I will use it everywhere. 前言 - 我喜欢C ++ lambda,如果可能的话,我将在任何地方使用它。

Now I have a lambda requirement, I need to a __stdcall lambda. 现在我有一个lambda要求,我需要一个__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 ): 这是我的代码( EnumWindowsProc在函数范围内 ):

        auto EnumWindowsProc = 
            [&](HWND hwnd, LPARAM lParam) mutable -> bool
        {
            return true;
        };
        EnumWindows(EnumWindowsProc, NULL);

I just noticed you have the visual studio 2010 tag. 我刚刚注意到你有visual studio 2010标签。 Stateless lambdas were implemented in VC11. 无状态lambda在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. 在将lambdas投入工作文件(v0.9)并添加可变lambdas(v1.0)之后,标准化委员会对措辞进行了彻底修改,产生了lambdas v1.1。 This happened too late for us to implement in VC10, but we've already implemented it in VC11. 这对于我们在VC10中实施来说太晚了,但我们已经在VC11中实现了它。 The lambdas v1.1 wording clarifies what should happen in corner cases like referring to static members, or nested lambdas. lambdas v1.1措辞阐明了在引用静态成员或嵌套lambda等极端情况下应该发生什么。 This fixes a bunch of bugs triggered by complicated lambdas. 这修复了由复杂lambda引发的一堆错误。 Additionally, stateless lambdas are now convertible to function pointers in VC11. 此外,无状态lambda现在可以转换为VC11中的函数指针。 This isn't in N2927's wording, but I count it as part of lambdas v1.1 anyways. 这不是N2927的措辞,但无论如何我将它视为lambdas v1.1的一部分。 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." 这是FDIS 5.1.2 [expr.prim.lambda] / 6: “没有lambda-capture的lambda表达式的闭包类型有一个公共的非虚拟非显式const转换函数,指向具有相同参数的函数,返回类型作为闭包类型的函数调用操作符。此转换函数返回的值应该是一个函数的地址,当调用它时,它与调用闭包类型的函数调用操作符具有相同的效果。 ( 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. ) 它甚至比那更好,因为我们已经将无状态lambda转换为具有任意调用约定的函数指针。这在处理期望__stdcall函数指针的API时非常重要。

Also, note that this conversion happens when there is no capture specification as mentioned in the second bolded quote. 另请注意,如果没有第二个粗体引用中提到的捕获规范,则会发生此转换。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM