unresolved_external
unresolved_external

Reputation: 2018

What is the syntax for a function-pointer typedef?

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

But it is not as easy as it seems to be:

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

When I tried it that way I got:

error C3646: 'task' : unknown override specifier

error, after playing for a little while with this declaration I'm stuck and can't find any reasonable solution to use to declare such type of typedef.

Upvotes: 6

Views: 1560

Answers (3)

pb2q
pb2q

Reputation: 59617

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

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

task is now a type alias for: pointer to a function taking a void pointer and returning unsigned.

Upvotes: 15

Dude
Dude

Reputation: 583

Sidenote: The __stdcall part might break your code under different compilers / compiler settings (unless the function is explicitly declared as __stdcall, too). I'd stick to the default calling convention only using proprietary compiler extensions having good reaons.

Upvotes: 0

Matthieu M.
Matthieu M.

Reputation: 299910

Since hmjd's answer has been deleted...

In C++11 a whole newsome alias syntax has been developed, to make such things much easier:

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

is equivalent the 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 quite nicer than the old one (and for templates, actually makes template aliasing possible) but does require a quite newish compiler.

Upvotes: 7

Related Questions