-4

From my understanding, typedef syntax is:

typedef existing_type new_type_name;

However, in Chrome's v8 namespace, there are many typedefs that seem to work with some other syntax. For example,

typedef void(*  FunctionCallback )(const FunctionCallbackInfo< Value > &info)

There are two things I don't understand:
1- FunctionCallbackInfo< Value > is a class defined in the namespace, while FunctionCallback isn't; isn't the existing type supposed to come first?
2- There are no spaces seperating the existing_type and new_type_name

What is the right way to read this?

EDIT: I am taking all typedef information in this tutorial.

plafer
  • 185
  • 12
  • 3
    Give this a read: http://stackoverflow.com/questions/4295432/typedef-function-pointer – user4581301 May 28 '15 at 00:53
  • btw, many people prefer alias declarations instead of typedefs. For those the syntax is more straightforward, `using new_name = existing_type_id;`. – Brian Bi May 28 '15 at 01:02

3 Answers3

8

Your understanding of the typedef syntax is a bit inaccurate. It's not:

typedef existing_type new_type_name;

It's more like:

typedef declaration;

So your example is a declaration of a typedef for a function pointer type named FunctionCallback, which returns void and takes a reference to a const FunctionCallbackInfo< Value > as its only parameter.

Carl Norum
  • 219,201
  • 40
  • 422
  • 469
  • 1
    Yep. Basically, prefixing any valid declaration with `typedef` creates a type alias instead of a variable. – zneak May 28 '15 at 00:57
4

The best way to understand a typedef is not to think of it as

typedef existing_type new_type_name;

The best way to understand this is to compare it with a regular variable or a class member declaration. For example:

char *ptr_func;

This declares ptr_func as a pointer to a character. Now, when you stick a typedef in front of that, you're essentially turning this into a type definition, not a variable declaration:

typedef char *ptr_func;

This now declares ptr_func as a type that represents a pointer to a character.

Now, let's look at your example as a variable declaration:

void(*FunctionCallback )(const FunctionCallbackInfo< Value > &info);

What is this? Well, this declares a FunctionCallback which is a pointer to a function that takes a const FunctionCallbackInfo<value> & parameter, returning a void.

So, it logically follows that sticking a typedef in front of this turns this into a definition of FunctionCallback as a type which is a pointer to a function that takes a const FunctionCallbackInfo<value> & parameter, returning a void.

Sam Varshavchik
  • 114,536
  • 5
  • 94
  • 148
1
typedef void(*  FunctionCallback )(const FunctionCallbackInfo< Value > &info)

defines the type FunctionCallback as a pointer to a function taking const FunctionCallbackInfo< Value > & and returning void. I keep promoting this rule, which I find extremely useful in reading complicated pointer declarations: The ``Clockwise/Spiral Rule''. Oldies but goldies ;)

vsoftco
  • 55,410
  • 12
  • 139
  • 252