6

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.

5
  • 1
    what should "task" mean? is it some special keyword? Commented Aug 30, 2012 at 13:21
  • Task is a word which I want to use instead of this long declaration. But nevermind, the solution is found. Thanks to all, specially pb2q :) Commented Aug 30, 2012 at 13:49
  • Be warned: Implementig a reasonably good thread pool is far from trivial. Did you consider to use an existing implementation? Commented Aug 30, 2012 at 14:05
  • Well it is in educational purposes, so I think I will take a risk :) Commented Aug 30, 2012 at 14:11
  • @unresolved_external: and you didn't wonder what start_address was for? Commented Aug 30, 2012 at 14:34

3 Answers 3

15

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.

Sign up to request clarification or add additional context in comments.

3 Comments

@unresolved_external Don't forget to accept this answer if is the best solution to your problem (which from your comment it seems it is).
@unresolved_external: Forgetting function pointer syntax in C is nothing to be ashamed of :D
that sucks ... i like C++, but the syntax is really retarded, when it comes to pointers and constants
7

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.

Comments

0

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.

1 Comment

Or use a macro that is #define'd to either __stdcall or something else depending on the compiler. Although typically you only explicitly declare the calling convention when interacting with a (usually platform-specific) library that has a defined calling convention.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.