What is the syntax for a typedef function pointer?

I am working on a thread pool and avoiding long qualifier names, I would like to use a typedef declaration.

But it is not as simple as it seems:

 typedef unsigned ( __stdcall *start_address )( void * ) task; 

When I tried this, I got:

 error C3646: 'task' : unknown override specifier 

after some time playing with this ad I got stuck and can't find a reasonable solution to declare this typedef type.

+6
source share
3 answers

When creating a typedef alias for a function pointer, the alias is in the position of the function name, so use:

 typedef unsigned (__stdcall *task )(void *); 

task now an alias of type: a pointer to a function with a void pointer and returns unsigned .

+15
source

Since hmjd answer has been deleted ...

In C ++ 11, a whole syntax of aliases was developed to make such things a lot easier:

 using task = unsigned (__stdcall*)(void*); 

equivalent to typedef unsigned (__stdcall* task)(void*); (note the position of the alias in the middle of the function signature ...).

It can also be used for templates:

 template <typename T> using Vec = std::vector<T>; int main() { Vec<int> x; } 

This syntax is rather pleasant than the old one (and for templates, it actually makes it possible to overlay templates), but it requires a completely new compiler.

+7
source

Sidenote: the __stdcall part can break your code under different compiler / compiler settings (if the function is not explicitly declared as __stdcall ) too. I adhere to the default usage convention only using proprietary compiler extensions with good explanations.

0
source

Source: https://habr.com/ru/post/924174/


All Articles