Complex Typedef in C ++

I understand that typedef can be used to define a new custom type, for example:

// simple typedef
typedef unsigned long ulong;

// the following two objects have the same type 
unsigned long l1;
ulong l2;

I recently stumbled upon this typedef and got lost in deciphering what is happening in the declaration:

typedef int16_t CALL_CONVENTION(* product_init_t)(product_descript_t *const description)

Can someone help me and explain what this does?

EDIT: changed the value of NEW_TYPE to CALL_CONVENTION. This is the definition. Thank you for noticing this.

+4
source share
3 answers

It declares a type product_init_tas a pointer to a function that

  • accepts a parameter product_descript_t *const description;
  • returns int16_t;
  • uses a calling convention CALL_CONVENTION(like @MM , suggested even when it was wrong).

PS " 2016 type-alias" (@Howard Hinnant), :

using product_init_t = int16_t (CALL_CONVENTION *)(product_descript_t *const description);
+8

typedefs: typedef :

int16_t NEW_TYPE (* product_init_t)(product_descript_t *const description);

, : product_init_t .

, typedef , product_init_t , .


, cdecl, , (, , ).

- , , . :

returntype    function_name   (parameters_opt)

, __declspec , , ; extern "C" ..

, (*product_init_t) int16_t NEW_TYPE ( declspecs) (product_descript_t *const description).

, * product_init_t - , , product_init_t .

, NEW_TYPE. , ( ); , gcc -E , IDE .

+2

#define typedef.

#define FX_TYPE void (*)(int)
typedef void (*stdfx)(int);

void fx_typ(stdfx fx); /* ok */
void fx_def(FX_TYPE fx); /* error */

.

:

typedef int16_t CALL_CONVENTION(* product_init_t)(product_descript_t *const description)

typedef :

void fx_typ(product_init_t fx);

fx , product_descript_t *const description int16_t.

CALL_CONVENTION , :

#define  CALL_CONVENTION

Note: the above micro has no body. In my opinion, it CALL_CONVENTIONjust adds confusion.

+1
source

All Articles