Defining alias templates
In C++, an alias is a name used to refer to a type that has been previously defined, whether a built-in type or a user-defined type. The primary purpose of aliases is to give shorter names to types that have a long name or provide semantically meaningful names for some types. This can be done either with a typedef
declaration or with a using
declaration (the latter was introduced in C++11). Here are several examples using typedef
:
typedef int index_t; typedef std::vector< std::pair<int, std::string>> NameValueList; typedef int (*fn_ptr)(int, char); template <typename T> struct foo { typedef T value_type; };
In this example, index_t
is an alias for int
, NameValueList
is an alias for std::vector<std::pair<int, std::string>>
, while fn_ptr
is an alias for the type of a pointer to a function that returns an int
and has two parameters of type int...