More complex uses of typedef

suggest change

The rule that typedef declarations have the same syntax as ordinary variable and function declarations can be used to read and write more complex declarations.

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"

This is especially useful for constructs with confusing syntax, such as pointers to non-static members.

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"

It is hard to remember the syntax of the following function declarations, even for experienced programmers:

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

typedef can be used to make them easier to read and write:

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

Feedback about page:

Feedback:
Optional: your email if you want me to get back to you:


Typedef and type aliases:
* More complex uses of typedef

Table Of Contents
8 Arrays
11 Loops
39 Streams
51 Unions
56 Lambdas
60 SFINAE
62 RAII
67 Sorting
84 RTTI
87 Scopes
104 Profiling
107 Recursion
117 Iteration
125 Alignment
130 Typedef and type aliases
134 Semaphore
136 Debugging
139 Mutexes
142 decltype