作用:推到出函数的返回类型
C++定义一个函数
第一种方式:
return-type fun(...){...}
如:int fun(...){...}
第二种方式;
auto fun(...)->return-type{...}
如:auto fun()->int{...}
第三种方式(模板):
使用decltype进行返回类型推导,无法返回模板参数
//这种方式无法做到
template<typename T1,typename T2>
decltype(a+b) fun(T1 a,T2 b){...}
//需要使用declval()进行辅助推导
template<typename T1,typename T2>
decltype(std::declval<T1>()+std::declval<T2>()) fun(T1 a,T2 b){...}
//但是上面太麻烦了,所以就加了个箭头
template<typename T1,typename T2>
auto fun(T1 a, T2 b)->decltype(a+b){...}