🍺既然有auto了,为什么又要有decltype呢?
auto 和decltype关键字都是用于自动类型推断,既然有auto了,为什么还要引出decltype呢,同样是为了解决前者无法满足的使用场景。
首先看一下两者的用法上的区别。
int iValue = 10;
auto auto_i = iValue;
decltype(iValue) decl_i = 100;
auto 根据=右边变量iValue的初始值推导出变量的类型为int,而 decltype 根据 ()内的iValue推导出变量的类型,跟=右边的100 没有关系。 decltype()括号内可以是任意复杂的表达式,但是不能是 void。
通过以上例子可以发现,两者的用途不一样.,auto 推导的类型只有编译器知道,而decltype 用来把推导出来的类型使用在其他地方。以下列举,必须使用decltype的场景。
👍需要某个表达式的返回值类型而又不想实际执行它时
string fun(string in)
{
cout << "fun:"<<in<<endl;
return in;
}
auto auto_r =fun("auto"); //运行时需要实际执行fun()
decltype(fun("decltype")) decl_r; //编译期类型推导fun()返回类型
运行只会输出auto 的调用结果:
auto用于decltype编译器只会分析表达式的类型,表达式实际不会进行运算,
👍模板函数返回值依赖模板参数的变量类型
这个在文章c++11 auto详解中举例过。
template <typename _Tx, typename _Ty>
auto add(_Tx x, _Ty y)->decltype(x+y)
{
return x+y;
}
auto在这里的作用只是为函数返回值占了一个位置,真正的返回值是后面的decltype(x+y)。
当然,c++14以后,可以直接这样写
template <typename _Tx, typename _Ty>
auto add(_Tx x, _Ty y)
{
return x+y;
}
👍类型推导时不想忽略引用和顶层const修饰
decltype和auto不同,它不会忽略引用和顶层const修饰。例如推导const类型
const int ivalue=8;
auto auto_v=ivalue; //等价于int auto_v=ivalue;
auto_v=9; //正确
decltype(ivalue) decl_v=9; //等价于const int decl_v=9
decl_v=10; //报错:decl_v是const的
例如推导引用类型
int a = 1;
int &ivalue=a;
auto auto_v=ivalue; //等价于int &auto_v=ivalue;
auto_v=9; //正确
decltype(ivalue) decl_v = 10; //报错:decl_v是int&的