请问C ++ 0x的支持__stdcall或EXTERN" C"捕获lambda表达式什么? [英] Will C++0x support __stdcall or extern "C" capture-nothing lambdas?

查看:125
本文介绍了请问C ++ 0x的支持__stdcall或EXTERN" C"捕获lambda表达式什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

昨天,我在想,是否有可能使用的C ++ 0x lambda函数的便捷写回调的Windows API函数。

Yesterday I was thinking about whether it would be possible to use the convenience of C++0x lambda functions to write callbacks for Windows API functions.

例如,如果我想使用lambda作为 EnumChildProc EnumChildWindows ?是这样的:

For example, what if I wanted to use a lambda as an EnumChildProc with EnumChildWindows? Something like:

EnumChildWindows(hTrayWnd, CALLBACK [](HWND hWnd, LPARAM lParam) {
        // ...
        return static_cast<BOOL>(TRUE); // continue enumerating
    }, reinterpret_cast<LPARAM>(&myData));

另一个用途是写的externC为C例程回调。例如:

my_class *pRes = static_cast<my_class*>(bsearch(&key, myClassObjectsArr, myClassObjectsArr_size, sizeof(my_class), extern "C" [](const void *pV1, const void *pV2) {
        const my_class& o1 = *static_cast<const my_class*>(pV1);
        const my_class& o2 = *static_cast<const my_class*>(pV2);

        int res;
        // ...
        return res;
    }));

这可能吗?

我可以理解lambda表达式捕获变量绝不会用C兼容,但它至少看起来可能我的捕捉全无的lambda表达式可以兼容。

I can understand that lambdas that capture variables will never be compatible with C, but it at least seems possible to me that capture-nothing lambdas can be compatible.

推荐答案

Lambda表达式没有捕获的是隐式转换的指针功能(由闭合类型定义的非显式转换功能)。

Lambdas without a capture are implicitly convertible to a pointer to function (by a non-explicit conversion function defined by the closure type).

该FCD似乎并没有规定什么语言联动的函数指针类型的函数类型都有,所以如果你需要通过这个函数指针的C函数,C的调用约定++函数和C函数必须是相同的。我相信,在Windows上,是这样的话,虽然。所以,你应该能够在lambda传递给Windows API函数

The FCD does not seem to specify what language linkage the function type of that function pointer type has, so if you need to pass this function pointer to C functions, the calling convention of C++ functions and C functions need to be the same. I believe that on Windows, that is the case though. So you should be able to pass the lambda to Windows API functions

typedef void(*callbackType)(void *userData);
extern "C" void someCFunction(callbackType callback);

int main() {
  someCFunction([](void *userData) { /* ... */ });
}

5.1.2 / 6 FCD写法


  

对于没有拉姆达捕获一个lambda-EX pression闭合类型有一个公共非虚非明示const的转换功能函数指针具有相同的参数和返回类型为闭合型的函数调用操作符。通过这种转换函数的返回值应是一个函数,调用时,有同一封FF等为调用闭合型的函数调用操作符的地址。

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.

我觉得最终的标准应该有一张纸条,上面说有一个转换功能C链接的函数指针和C ++链接函数指针都作为兑换到C函数指针是这个功能的目标之一。

I think the final Standard should have a note that says that there is a conversion function to both C linkage function pointers and C++ linkage function pointers, as convertibility to C function pointers is one of the goal of this functionality.

这篇关于请问C ++ 0x的支持__stdcall或EXTERN&QUOT; C&QUOT;捕获lambda表达式什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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