函数指针和调用约定
float __stdcall (*pFunc)(float a, float b) = (float (__stdcall *)(float,float))0x411280;
如何使用调用约定声明函数指针?上面给了我一个错误。
float __stdcall (*pFunc)(float a, float b) = (float (__stdcall *)(float,float))0x411280;
How to declare a function pointer with calling convention? The above gives me an error.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
技巧是将 __stdcall 放在括号内,如下所示:
当然,建议您使用 typedef 代替,但同样的技巧也适用:
The trick is placing the __stdcall inside the parentheses like this:
Of course, you are recommended to use a typedef instead, but the same trick applies:
对于常规函数,您通常可以这样做:
编译器是否接受所有这些形式是由实现定义的。铿锵的。对我来说,第五个版本最具语义意义,因为它是函数的属性,而不仅仅是返回类型。
您可以使用
__attribute__((cdecl))
而不是__cdecl
来完成所有这些操作,它也可以在函数之后使用,与__cdecl
现在 不同,声明一个指向具有特定调用约定的函数的常量指针
pfunc
:请注意,
const
必须像往常一样位于星号后面。对于双函数指针,指针可以根据调用约定到达任何地方,但您必须根据const
将其放置在正确的位置。同样,它是编译器接受的形式定义的实现。 Clang 接受所有形式并正确地将它们解释为类型
const int (*const)() __attribute__((cdecl))
For a regular function, you can usually do:
It is implementation defined whether or not a compiler will accept all of these forms. Clang does. To me, the 5th version makes the most semantic sense, because it is a property of the function and not the return type alone.
You can do all of this with
__attribute__((cdecl))
instead of__cdecl
, which can also be used after the function, unlike__cdecl
Now, to declare a constant pointer
pfunc
to a function with a specific calling convention:Note that
const
has to be after the asterisk as usual. With double function pointers, the pointers can go anywhere with respect to the calling convention, but you have to put it in the correct place with respect toconst
.Again, it is implementation defined as to what form a compiler accepts. Clang accepts all forms and correctly interprets them as the type
const int (*const)() __attribute__((cdecl))
__fastcall
是优化的(最快的调用约定),但由于未知原因未使用尝试:
__fastcall
is the optimized one (fastest calling convention) but not used for an unknown reasonTry: