user3633207
user3633207

Reputation:

Typedef a function pointer

I have two possibly simple questions as far as the code below is concerned:

#include <windows.h>//IN and OUT prefixes are defined here

typedef int(__cdecl *FOO)(IN int input);
int my_int_func(IN int x) {
    return x;
}

int main() {
    int res = 0;
    FOO foo = &my_int_func;
    res = (*foo)(5);
    return 0;
}

The code actually works flawlessly. Here are the questions:

1) What does the IN prefix mean in the typedef row? The prefix is defined in windows.h header. The code hangs if the header is omitted.

2) If I change __cdecl calling convention to __stdcall, the compiler underlines the &my_int_func and outputs the error message "Error: a value of type "int (*)(int x)" cannot be used to initialize an entity of type "FOO." My question is why?

I use MS Visual Studio Ultimate 2013.

Upvotes: 0

Views: 123

Answers (1)

user694733
user694733

Reputation: 16043

  1. IN and OUT are most likely indications for direction of parameter. They might serve as documentation for the user or 3rd party tools, or they could be hints to compiler to aid in optimization. In any case, those are not portable, and you must consider if they are worth using.

  2. Calling convention determines how function parameters are passed, how return value is passed, and where the cleanup happens after the function call is over. You obviously cannot mix different calling conventions, and that is what compiler is complaining about. Calling conventions are also not very portable, so you shouldn't specify it, unless you have a good reason to do so.

Upvotes: 2

Related Questions