Given your additional information about templates, we can now answer.
The use-case is when you want to specialize on the type of a template. One typical example is the following:
template <typename T>
struct nonconst {
typedef T t;
};
template <typename T>
struct nonconst<T const> {
typedef T t;
};
This effectively allows you to remove the const
qualifier from any type:
nonconst<int>::t x;
nonconst<int const>::t y;
assert(typeid(x) == typeid(int));
assert(typeid(y) == typeid(int));
There are many similar use-cases, e.g. to add (or remove) the pointer qualifier from a type, provide defaults and specializations for certain types, etc.
However, notice the different casing of the type names! Equal types in typedef T T
are illegal C++.[I stand corrected: §7.1.3.2] Furthermore, the de-fact naming standard (cemented by its use in Boost libraries) is to call the type name alias type
, e.g.:
typedef T type;