ThunderPhoenix
ThunderPhoenix

Reputation: 1883

How to define function type with typedef?

I would like to know if it is possible to define a function type using typedef, I tried this syntax:

typedef int (*) (void *, void *) order;

But it doesn't work. Error message : expected identifier or '(' before ')' token

Upvotes: 5

Views: 12469

Answers (4)

Vlad from Moscow
Vlad from Moscow

Reputation: 310950

This typedef

typedef int (*order )(void *, void *);

defines an alias for the type of pointer to function of the type int( void *, void * ).

This typedef

typedef int order(void *, void *);

defines an alias for the type of function of the type int( void *, void * ).

If to use one typedef to define both the alias for pointer and the alias for function you can write

typedef int ( *porder )( void *, void * ), order(void *, void *);

or even the following way

int typedef ( *porder )( void *, void * ), order(void *, void *);

Pay attention to that you can use either typedef name as a type specifier for a function parameter. For example these two declarations

void h( porder arg );
void h( order arg );

declare the same one function because the compiler adjusts implicitly the type of a parameter having a function type to pointer to the function type.

However you may not use interchangeably these typedef(s) for the function return type because functions may not return functions but they may return pointers to functions. So this declaration

porder h( void );

will compile. But this declaration

order h( void );

will not compile.

Without the typedef the declaration of a function that returns pointer to other function would be complicated. For example this declaration

porder h( void );

without the typedef looks like

int ( *h( void ) )( void *, void * );

Upvotes: 7

Lundin
Lundin

Reputation: 213711

You declare a function pointer just like a function, except you wrap the function name in parenthesis and write a * in front of the name.

void func (void);              // function
void (*func) (void);           // pointer to function
typedef void (*func) (void);   // pointer to function type

However, my recommended style is one that doesn't hide pointers behind a typedef:

typedef int order_t (void*, void*);
...
order_t* order; 

The typedef is a function template and the declaration is a function pointer declaration of that type. This makes function pointer syntax consistent with object pointers.

Upvotes: 2

glglgl
glglgl

Reputation: 91017

You do either of

typedef int (*order) (void *, void *);
typedef int order(void *, void *);

depending on if you want to typedef the function or the pointer.

Both has advantages and disadvantages; I prefer a style where a pointer is visible as such and have my functions defined as

order order_impl;
int order_impl(void * a, void * b)
{
}
order * order_pointer = order_impl;

This ensures that the function is defined properly.

Others, however, prefer working with the function pointer type and do

porder order_pointer = order_impl;

and live without the said safeguard, leaving them with only a warning instead of an error in the case of a mismatch.

Upvotes: 2

HolyBlackCat
HolyBlackCat

Reputation: 96091

The alias name should be placed where a variable name would be if it was a variable declaration.

Here:

typedef int (*order) (void *, void *);
//            ^~~~~

Or, if you want a function type rather than a pointer-to-function:

typedef int order(void *, void *);

Upvotes: 16

Related Questions