Type alias is a name that refers to a previously defined type (similar to typedef).
Alias template is a name that refers to a family of types.
Contents1 Syntax2 Explanation3 Notes4 Keywords5 Example6 Defect reports7 See also[edit] SyntaxAlias declarations are declarations with the following syntax:
using identifier attr (optional) = type-id ; (1) templateusing identifier attr (optional) = type-id ;
(2) template requires constraintusing identifier attr (optional) = type-id ;
(3) (since C++20) attr - optional sequence of any number of attributes identifier - the name that is introduced by this declaration, which becomes either a type name (1) or a template name (2) template-parameter-list - template parameter list, as in template declaration constraint - a constraint expression which restricts the template parameters accepted by this alias template type-id - abstract declarator or any other valid type-id (which may introduce a new type, as noted in type-id). The type-id cannot directly or indirectly refer to identifier. Note that the point of declaration of the identifier is at the semicolon following type-id.[edit] Explanation1) A type alias declaration introduces a name which can be used as a synonym for the type denoted by type-id. It does not introduce a new type and it cannot change the meaning of an existing type name. There is no difference between a type alias declaration and typedef declaration. This declaration may appear in block scope, class scope, or namespace scope.2) An alias template is a template which, when specialized, is equivalent to the result of substituting the template arguments of the alias template for the template parameters in the type-id.templatestruct Alloc {}; templateusing Vec = vector; // type-id is vector Vec v; // Vec is the same as vectorWhen the result of specializing an alias template is a dependent template-id, subsequent substitutions apply to that template-id:
templateusing void_t = void; templatevoid_t f(); f(); // error, int does not have a nested type fooThe type produced when specializing an alias template is not allowed to directly or indirectly make use of its own type:
templatestruct A; templateusing B = typename A::U; // type-id is A::U templatestruct A { typedef B U; }; B b; // error: B uses its own type via A::UAlias templates are never deduced by template argument deduction when deducing a template template parameter.
It is not possible to partially or explicitly specialize an alias template.Like any template declaration, an alias template can only be declared at class scope or namespace scope.
The type of a lambda expression appearing in an alias template declaration is different between instantiations of that template, even when the lambda expression is not dependent.
templateusing A = decltype([] {}); // A and A refer to different closure types(since C++20)[edit] NotesFeature-test macroValueStdFeature__cpp_alias_templates200704L(C++11)Alias templates[edit] Keywordsusing
[edit] ExampleRun this code#include #include #include #include // type alias, identical to// typedef std::ios_base::fmtflags flags;using flags = std::ios_base::fmtflags;// the name 'flags' now denotes a type:flags fl = std::ios_base::dec; // type alias, identical to// typedef void (*func)(int, int);using func = void (*) (int, int); // the name 'func' now denotes a pointer to function:void example(int, int) {}func f = example; // alias templatetemplateusing ptr = T*;// the name 'ptr' is now an alias for pointer to Tptr x; // type alias used to hide a template parametertemplateusing mystring = std::basic_string; mystring str; // type alias can introduce a member typedef nametemplatestruct Container { using value_type = T; }; // which can be used in generic programmingtemplatevoid info(const ContainerT& c){typename ContainerT::value_type T;std::cout