通过模板推导,std::vector<int&> 可行吗?
With template deduction, std::vector<int&> is feasible?
我正在学习C++,现在正在进行引用折叠。然而,我现在有一个疑问:
template <typename T>
std::vector<T>& Foo(T) // value of parameter is unnecessary
{
static std::vector<T> s_vec_T;
return s_vec_T;
}
int main()
{
int i = 0;
auto get_vec = Foo(i); // suppose Foo() would be Foo<int&>(i)
return 0;
}
因此,最终Foo()
s_vec_T
中的静态向量将被实例化为std::vector<int&>
。尽管如此,代码运行良好。为什么?我想,没有什么可以处理的参考崩溃。
T
将被推导为int
(i
是int
,所以这是将被推导的类型),所以没有问题。std::vector
不能保存引用,所以如果它以某种方式将其推断为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])'没有匹配函数
- 错误:调用"std::vector<:vector<int>>::p ush_back(std::vector<std::__cxx11::basic_string<
- 为什么将此对向量&lt; map&lt; int,int&gt;&gt;中的地图进行更新.失败
- 如何加入向量&lt; int&gt;到C 中的单个INT
- 是numeric_limits&lt; int&gt; :: is_modulo从逻辑上矛盾
- 运营商&lt;&lt;无法访问班级的私人int
- std::pair的默认构造函数<>将基本类型(int等)设置为零
- 如何显示/打印字符串对象?cout & lt; & lt;Int工作,count <<字符串