如何指定 vc11 lambda 调用约定 [英] how to specify vc11 lambda calling convention

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

问题描述

我想将嵌套在类中的 lambda 函数指针传递给 Windows API 回调函数.我发现没有地方可以指定 __stdcall 关键字.有人告诉我编译只支持__cdecl,但是我用nm命令转储obj文件后,发现编译会生成三个辅助函数(__stdcall>__cdecl, __fastcall) 同时进行.所以我的问题是,如何指定调用约定?

I want to pass a lambda function pointer, which nested in a class, to the Windows API callback function. I found there is no place for me to specify the __stdcall keyword. Some people told me the compile only support __cdecl, but after I used nm command to dump the obj file, I found the compile will generate three helper function (__stdcall, __cdecl, __fastcall) concurrently. So my problem is, how can I specify the calling convention?

以下代码是我的测试代码.

Those following code are my test code.

#include "stdafx.h"
int _tmain(int argc, _TCHAR* argv[])
{
    auto func = [](){};
    return 0;
}

00000000 t ?<helper_func_cdecl>@<lambda_5738939ec88434c53e1a446c47cf2db6>@@CAXXZ
00000000 t ?<helper_func_fastcall>@<lambda_5738939ec88434c53e1a446c47cf2db6>@@CIXXZ
00000000 t ?<helper_func_stdcall>@<lambda_5738939ec88434c53e1a446c47cf2db6>@@CGXXZ
00000000 t ??B<lambda_5738939ec88434c53e1a446c47cf2db6>@@QBEP6AXXZXZ
00000000 t ??B<lambda_5738939ec88434c53e1a446c47cf2db6>@@QBEP6GXXZXZ
00000000 t ??B<lambda_5738939ec88434c53e1a446c47cf2db6>@@QBEP6IXXZXZ
00000000 t ??R<lambda_5738939ec88434c53e1a446c47cf2db6>@@QBEXXZ

推荐答案

Cast it:

WinApiFunc(static_cast<void(__stdcall *)()>(func));

或者先存入局部变量:

void (__stdcall *funcp)() = func;
WinApiFunc(funcp);

这篇关于如何指定 vc11 lambda 调用约定的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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