类型别名、别名模板 (C++11 起)
来自 cppreference.cn
类型别名是引用先前定义的类型的名称(类似于 typedef
)。
别名模板是引用一族类型的名称。
目录 |
[编辑] 语法
别名声明是具有以下语法的声明
using identifier attr (可选) = type-id ; |
(1) | ||||||||
template < template-parameter-list >
|
(2) | ||||||||
template < template-parameter-list > requires constraint
|
(3) | (C++20 起) | |||||||
属性 | - | 任意数量的属性的可选序列 |
标识符 | - | 此声明引入的名称,它成为类型名称 (1) 或模板名称 (2) |
模板参数列表 | - | 模板参数列表,如模板声明中所示 |
约束 | - | 一个约束表达式,它限制了此别名模板接受的模板参数 |
类型标识 | - | 抽象声明符或任何其他有效的 type-id(可能引入新类型,如type-id中指出)。type-id 不能直接或间接引用 identifier。请注意,标识符的声明点位于 type-id 之后的Semicolon。 |
[编辑] 解释
1) 类型别名声明引入了一个名称,该名称可以用作 type-id 所表示类型的同义词。它不引入新类型,也不能改变现有类型名称的含义。类型别名声明和 typedef 声明之间没有区别。此声明可以出现在块作用域、类作用域或命名空间作用域中。
2) 别名模板是一个模板,当其特化时,它等效于将别名模板的模板参数替换为 type-id 中的模板参数的结果。
template<class T> struct Alloc {}; template<class T> using Vec = vector<T, Alloc<T>>; // type-id is vector<T, Alloc<T>> Vec<int> v; // Vec<int> is the same as vector<int, Alloc<int>>
当特化别名模板的结果是依赖template-id时,后续替换适用于该template-id
template<typename...> using void_t = void; template<typename T> void_t<typename T::foo> f(); f<int>(); // error, int does not have a nested type foo
特化别名模板时产生的类型不允许直接或间接使用其自身的类型
template<class T> struct A; template<class T> using B = typename A<T>::U; // type-id is A<T>::U template<class T> struct A { typedef B<T> U; }; B<short> b; // error: B<short> uses its own type via A<short>::U
在推导模板模板参数时,模板参数推导从不推导别名模板。
不可能部分或显式特化别名模板。与任何模板声明一样,别名模板只能在类作用域或命名空间作用域中声明。
别名模板声明中出现的lambda 表达式的类型在模板实例化之间是不同的,即使 lambda 表达式不依赖也是如此。 template<class T> using A = decltype([] {}); // A<int> and A<char> refer to different closure types |
(C++20 起) |
[编辑] 注意
功能测试宏 | 值 | 标准 | 特性 |
---|---|---|---|
__cpp_alias_templates |
200704L |
(C++11) | 别名模板 |
[编辑] 关键词
[编辑] 示例
运行此代码
#include <iostream> #include <string> #include <type_traits> #include <typeinfo> // 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 template template<class T> using ptr = T*; // the name 'ptr<T>' is now an alias for pointer to T ptr<int> x; // type alias used to hide a template parameter template<class CharT> using mystring = std::basic_string<CharT, std::char_traits<CharT>>; mystring<char> str; // type alias can introduce a member typedef name template<typename T> struct Container { using value_type = T; }; // which can be used in generic programming template<typename ContainerT> void info(const ContainerT& c) { typename ContainerT::value_type T; std::cout << "ContainerT is `" << typeid(decltype(c)).name() << "`\n" "value_type is `" << typeid(T).name() << "`\n"; } // type alias used to simplify the syntax of std::enable_if template<typename T> using Invoke = typename T::type; template<typename Condition> using EnableIf = Invoke<std::enable_if<Condition::value>>; template<typename T, typename = EnableIf<std::is_polymorphic<T>>> int fpoly_only(T) { return 1; } struct S { virtual ~S() {} }; int main() { Container<int> c; info(c); // Container::value_type will be int in this function // fpoly_only(c); // error: enable_if prohibits this S s; fpoly_only(s); // okay: enable_if allows this }
可能的输出
ContainerT is `struct Container<int>` value_type is `int`
[编辑] 缺陷报告
下列更改行为的缺陷报告追溯地应用于以前出版的 C++ 标准。
缺陷报告 | 应用于 | 发布时的行为 | 正确的行为 |
---|---|---|---|
CWG 1558 | C++11 | 别名特化中未使用的参数 是否参与替换未指定 |
替换 已执行 |
[编辑] 另请参阅
typedef 声明
|
为类型创建同义词 |
命名空间别名 | 创建现有命名空间的别名 |