西安网络优化培训机构公司,seo优化软件购买,阿里巴巴网站域名注册,app推广多少钱一单1. 格式说明
C 11 标准之后开始有这种用法。
类型别名是指代先前定义的类型的名称#xff08;类似于 typedef #xff09;。别名模板是指代类型族的名称。
声明的语法格式#xff1a;
using identifier attr #xfeff;(optional) type-id ;(1)template te…1. 格式说明
C 11 标准之后开始有这种用法。
类型别名是指代先前定义的类型的名称类似于 typedef 。别名模板是指代类型族的名称。
声明的语法格式
using identifier attr (optional) type-id ;(1)template template-parameter-list using identifier attr (optional) type-id ; (2)template template-parameter-list requires constraint using identifier attr (optional) type-id ; (3)(since C20)
当然上述两行的都可以写在一行中不过为了美观模板参数和 using 句子分两行。
attr-任意数量的可选属性序列。identifier-根据这个声明引入的名称, 其要么为一个类型名 (1) 要么是一个模板名 (2)。template-parameter-list-模板参数列表, 同模板声明的参数列表。constraint-一个约束表达式其限制这个别名模板接受的模板参数。type-id-抽象声明符或任意其它有效的类型标识符(其可以引入一个新的类型如在 type-id 中指出的那样)类型标识符不能直接或间接地引用修饰符注意修饰符的声明点位于 type-id 后面的分号处。
解释
(1) 类型别名声明引入一个名称该名称可用作 type-id 所表示类型的同义词。它不会引入新类型也不能改变现有类型名称的含义。类型别名声明与 typedef 声明之间没有区别。此声明可以出现在块作用域、类作用域或命名空间作用域中。 (2) 别名模板是一种模板其特化后等同于用别名模板的模板实参替换 type-id 中的模板形参。
templateclass T
struct Alloc {};
templateclass T
using Vec vectorT, AllocT; // type-id 是 vectorT, AllocT
Vecint v; // Vecint 与 vectorint, Allocint 相同
(3) 当特化别名模板的结果是一个依赖模板 ID 时后续替换将应用于该模板 ID
templatetypename...
using void_t void; templatetypename T
void_ttypename T::foo f(); fint(); // 错误, int 没有嵌入类型 foo
( 注templatetypename... 表示可变模板参数 )
(4) 特化别名模板时生成的类型不允许直接或间接使用其自身的类型
templateclass T
struct A; templateclass T
using B typename AT::U; // type-id 是 AT::U templateclass T
struct A { typedef BT U; }; Bshort b; // 错: Bshort 通过 Ashort::U 使用了其自身的类型 5 在推导模板模板参数时别名模板永远不会通过模板实参推导来推导。不可能部分或显式地特化别名模板。 6与任何模板声明一样别名模板只能在类作用域或命名空间作用域内声明。 7 别名模板声明中出现的 lambda 表达式的类型在该模板的实例之间是不同的即使 lambda 表达式不依赖。
templateclass T
using A decltype([] {}); // Aint 和 Achar 指不同的闭包类型
2. 应用举例
#include iostream
#include string
#include type_traits
#include typeinfo // 类型别名, 等价于
// typedef std::ios_base::fmtflags flags;
using flags std::ios_base::fmtflags;
// 名称 flags 现在表示一个类型:
flags fl std::ios_base::dec; // 类型别名, 等价于
// typedef void (*func)(int, int);
using func void (*) (int, int); //名称 func 现在表示一个函数指针:
void example(int, int) {}
func f example; // 别名模板
templateclass T
using ptr T*;
// 现在 ptrT 表示一个T指针的别名
ptrint x; // 类型别名用于隐藏一个模板参数
templateclass CharT
using mystring std::basic_stringCharT, std::char_traitsCharT; mystringchar str; // 类型别名可以引入成员typedef 名
templatetypename T
struct Container { using value_type T; }; // 可用于泛型编程
templatetypename 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;
} // 类型别名用于简化 std::enable_if 的语法
templatetypename T
using Invoke typename T::type; templatetypename Condition
using EnableIf Invokestd::enable_ifCondition::value; templatetypename T, typename EnableIfstd::is_polymorphicT
int fpoly_only(T) { return 1; } struct S { virtual ~S() {} }; int main()
{ Containerint c; info(c); // Container::value_type 在此函数中将为 int // fpoly_only(c); // error: enable_if prohibits this S s; fpoly_only(s); // okay: enable_if 允许这样做
}
可能输出
ContainerT is struct Containerint
value_type is int