C++ Alias Templates
In C++11, a type alias is a name for another already declared type, and an alias template is a name for another already declared template. Both of these types of aliases are introduced with a new using
syntax.
This is an example of a type alias using using
:
using byte = unsigned char;
, instead of former:
typedef unsigned char byte;
This is an example of a type alias using using
:
template <class T> class custom_allocator { /* ... */}; template <typename T> using vec_t = std::vector<T, custom_allocator<T>>; vec_t<int> vi; vec_t<std::string> vs;
It is important to take note of the following things:
- Alias templates cannot be partially or explicitly specialized.
- Alias templates are never deduced by template argument deduction when deducing a template parameter.
- The type produced when specializing an alias template is not allowed to directly or indirectly make use of its own type.