为什么T不推断到int&& here这里
why T does not deduce to int&& here
下面是一个函数模板
template <class T>
void func(T&& t) {
}
func(4); // 4 is rvalue and T deduces to int
所以我的问题是为什么 T 不推断为 int&&?
我的意思是,如果T推导出为int&&
所以int&&&&->int&&,这对我来说也很有意义!
为什么
T
不推断int&&
?
如果模板参数是引用,则通常在考虑推导如何发生之前删除引用(例外情况是针对左值、函数和数组推导引用时转发引用(。
在这种情况下,T
是针对4
推导的,这是一个int
,所以T
推导为int
。生成的类型T&&
为 int&&
。
请注意,表达式永远不会有引用类型。 4
是类型int
的右值,它不是int&&
。
这与一般的演绎工作方式一致:
template <class T> void foo(T );
template <class T> void bar(T const& );
foo(4); // calls foo<int>, not foo<int&&>
bar(4); // calls bar<int>, not bar<int const&>
为什么要
推论int&&
?请注意,参数的类型是 T &&
,而不是 T
。所以:
- 所需的参数最终类型(类型推导的乘积(为
int &&
。 - 参数的形式形式为
T &&
。 int &&
T &&
最简单的T
是什么?
答:int
。
相关文章:
- 为什么在全局范围内使用"extern int a"似乎不行?
- int(c) 和 c-'0' 之间的区别。C++
- 从"int*"强制转换为"unsigned int"会丢失精度错误
- 为什么野牛仍在使用"int yylex(void)",却找不到"int yylex(YYS
- 有符号的int和int-有没有一种方法可以在C++中区分它们
- 请解释这句话(cout<<1+int((a<b)^((b-a)&1) )<<endl
- 是否可以从int转换为enum类类型
- 不能在初始值设定项列表中将非常量表达式从类型 'int' 缩小到'unsigned long long'
- 向量 <int> a {N, 0} 和 int arr a[N] = {0} 的时间复杂度有什么区别
- 'short int'持有的值溢出,但"自动"不会溢出?
- 如何在C++中将一个无符号的 int 转换为两个无符号的短裤?
- 调用'begin(int [n])'没有匹配函数
- 没有显式声明的int[]中的foreach
- 在c++中访问int到类对象的映射时出错
- 为什么我无法更改"set<set>"循环中的值<int>
- 长 长 int 不要 长 int 好
- C++程序在循环后给出奇怪的int值
- 如何计算数据类型的范围,例如int
- 如果"new int"返回"int*",那么为什么"new int[n]"不返回"int**"?
- 为什么T不推断到int&& here这里