Next: , Previous: Attributes, Up: Top


12 Typedefs

When and where are typedefs useful? Where may typedefs appear? Can be in namespace, or local to a definition.

Two kinds of typedefs, with three syntaces.

typedef-declaration:

  1. typedef existing-definition-id identifier;
  2. typedef existing-definition-id < template argument list > identifier;
  3. template-signature typedef existing-definition-id < template-argument-list > identifier;

In all three forms, the identifier is the name of the new typedef. The only prerequisite for the existing definition is that it has already been declared.

The first form is a pseudo-typedef, a pure definition name alias, as if one had written #define identifier existing-definition-id in C-preprocessing, with the existing defininition being a simple identifier. (existing-definition-id may be a relative or absolute hierarchical name.) The potential confusion with the first typedef is that existing-definition-id identifier may be mistaken for a templated type with all parameter arguments with defaults (the only condition in which template arguments may be omitted from a templated definition).

The second form of typedef substitutes a fully-specified template type (one with all template parameters supplied) with an identifier. This is convenient for reusing a templated type repeatedly without having to copy the arguments.

The third form of typedef is a template typedef (not yet supported in C++) which wraps a partially-specified template type with a new definition, usually (not always) with fewer arguments. This is particularly useful for binding template arguments to make simple template types. If a definition doesn't already have default arguments, this is one way of supplying additional default values for an existing templated definition.

Typedefs can be defined in terms of other typedefs. There is currently no restriction on the number of indirections of typedef-ing.

By construction, the graph of typedef definitions must form a tree, and thus, cannot have cycles. Ultimately, every typedef must be defined in terms of a unique non-typedef canonical definition. The canonical parameters are the values of the parameters that are eventually passed to the canonical definition, and are evaluated by the transformations defined by each typedef indirection.

Templates typedefs: should they be specializable? Methinks not: that would make things incredibly confusing.