如何使用__stdcall来限定C ++ lambda? [英] how to use __stdcall to qualify C++ lambda?

查看:146
本文介绍了如何使用__stdcall来限定C ++ lambda?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

前言 - 我喜欢C ++ lambda,如果可能,我会使用它无处不在。

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

现在我有一个lambda需求,我需要到__stdcall lambda。但是我得到以下错误消息:

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

可以帮助我吗?
这是我的代码( EnumWindowsProc在函数范围):

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


推荐答案

我只是注意到你有 visual studio 2010 标签。无状态lambdas在VC11中实现。 参考

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


在lambdas投票成为工作文件(v0.9)和mutable
lambdas之后(v1.0),标准化委员会大修了
的措辞, lambdas v1.1。 这对我们来说太晚了,我们在VC10中实现
,但是我们已经在VC11中实现了。

lambdas v1.1文字说明了
引用静态成员或嵌套lambdas。这修复了一堆由复杂lambdas触发的
错误。另外,无状态
lambdas现在可以转换为VC11中的函数指针。这不是
在N2927的措辞,但我算它作为lambdas v1.1的一部分。
它是FDIS 5.1.2 [expr.prim.lambda] / 6:没有lambda捕获的
lambda表达式的闭包类型有一个非虚拟的
非显式const转换函数到具有
的函数的指针与闭包类型的函数相同的参数和返回类型
调用操作符。
此转换函数返回的值应为
一个函数的地址,当被调用时,具有与
调用闭包类型的函数调用操作符相同的效果。 (甚至
比那更好,因为我们使无状态lambdas可转换为
函数指针与任意调用约定。这是
在处理API期望__stdcall函数$

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.

这篇关于如何使用__stdcall来限定C ++ lambda?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆