typedef 的更复杂用法

typedef 声明与普通变量和函数声明具有相同语法的规则可用于读取和写入更复杂的声明。

void (*f)(int);         // f has type "pointer to function of int returning void"
typedef void (*f)(int); // f is an alias for "pointer to function of int returning void"

这对于具有混淆语法的构造尤其有用,例如指向非静态成员的指针。

void (Foo::*pmf)(int);         // pmf has type "pointer to member function of Foo taking int
                               // and returning void"
typedef void (Foo::*pmf)(int); // pmf is an alias for "pointer to member function of Foo
                               // taking int and returning void"

很难记住以下函数声明的语法,即使对于有经验的程序员也是如此:

void (Foo::*Foo::f(const char*))(int);
int (&g())[100];

typedef 可用于使它们更易于读写:

typedef void (Foo::pmf)(int);  // pmf is a pointer to member function type
pmf Foo::f(const char*);       // f is a member function of Foo

typedef int (&ra)[100];        // ra means "reference to array of 100 ints"
ra g();                        // g returns reference to array of 100 ints