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.
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.
The trick is placing the __stdcall inside the parentheses like this:
float (__stdcall *pFunc)(float a, float b) = (float (__stdcall *)(float,float))0x411280;
Of course, you are recommended to use a typedef instead, but the same trick applies:
typedef float (__stdcall *FuncType)(float a, float b);
typedef
float
(__stdcall *FuncType)(float a, float b)
? –
Phio float __stdcall (* pFunc)(float a, float b)
–
Spurge For a regular function, you can usually do:
__cdecl const int func();
__cdecl const int (func)();
const __cdecl int func();
const int __cdecl func();
const int (__cdecl func)();
__cdecl const __cdecl int (__cdecl func)();
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
const int (__cdecl func)() __attribute__((cdecl));
Now, to declare a constant pointer pfunc
to a function with a specific calling convention:
__cdecl const int (*const pfunc)();
const __cdecl int (*const pfunc)();
const int __cdecl (*const pfunc)();
const int (__cdecl *const pfunc)();
const int (*const __cdecl pfunc)();
const int (*__cdecl const pfunc)();
__cdecl const __cdecl int (__cdecl *const pfunc)();
const int (*const pfunc)() __attribute__((cdecl));
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 to const
.
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))
typedef void* (__cdecl *byte_array_alloc_fn)(int count)
which compiles using MSVC, but produces errors in GCC. –
Scotism __attribute__((cdecl))
on gcc –
Stomach __fastcall
is the optimized one (fastest calling convention) but not used for an unknown reason
Try:
int (__fastcall *myfunction)(int,float);
© 2022 - 2024 — McMap. All rights reserved.