Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

complicated Typedef in C++

Tags:

c++

typedef

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 came across this typedef, and got lost in deciphering what is going on in the declaration:

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

Can someone guide me and explain what this is doing?

EDIT: changed NEW_TYPE to CALL_CONVENTION. It's a define. Thanks for spotting that out.

like image 950
nikk Avatar asked May 31 '16 20:05

nikk


1 Answers

It declares type product_init_t as a pointer to a function which

  • takes parameter product_descript_t *const description;
  • returns int16_t;
  • uses calling convention CALL_CONVENTION (as @M.M suggested even when it was misnamed).

P.S. Since "a complete answer in 2016 should show the modern way to write this type-alias" (@Howard Hinnant), here it is:

using product_init_t = int16_t (CALL_CONVENTION *)(product_descript_t *const description);
like image 91
AlexD Avatar answered Sep 28 '22 17:09

AlexD