Alias declaration support templatization, which avoids the “::type” suffix and “typename” prefix often required to refer typedef
s.
Compared with typedef
, alias declarations have following advantages:
Easier to understand
|
|
Support for template
Alias declarations may be templatized (called alias templates), while typedef
s cannot. For example, suppose we want to define a synonym for a linked list that uses a custom allocator MyAlloc
:
|
|
With a typedef
, to support template, we need the trick to nest typedef
inside templatized structs:
|
|
To make things worse, when using the typedef
inside a template class, we have to precede the typedef
name with typename
, so that compilers get comfirmed that MyAllocList<T>::type
refers to a type, instead of a data member named type
inside MyAllocList
:
|
|
Type traits supported by alias templates in C++14
When we need to take template type parameters and create revised types from them(e.g., turn Widget
into Widget&
), we perform these kinds of transformations through type traits. Since type traits in C++11 are implemented as nested typedef
s inside templatized structs, C++14 provides corresponding alias templates:
|
|
Basically what C++14 adds is simply some code like this:
|
|