decltype是C++11新增的一个关键字,和auto的功能一样,用来在编译时期进行自动类型推导。引入decltype是因为auto并不适用于所有的自动类型推导场景,在某些特殊情况下auto用起来很不方便,甚至压根无法使用。
decltype 可以推导表达式:
template <class A, class B>
auto findMin(A a, B b) -> decltype(a < b ? a : b)
{
return a < b ? a : b;
}
findMin函数,根据a,b中较小的值推导返回类型