C++11之decltype


  • decltype与auto关键字一样,用于进行编译时类型推导。
  • decltype实际上有点像auto的反函数,auto可以让你声明一个变量,而decltype则可以从一个变量或表达式中得到类型。
  • 注意,decltype 仅仅“查询”表达式的类型,并不会对表达式进行“求值”。
  • 注意,auto参数类型只能用在lambda上面,不能用在普通函数上面。
  • 例子1
int x = 1;
decltype(x) y = x;
  • 例子2
int    i;
float  f;
double d;
 
typedef decltype(i + f) type1;  // float
typedef decltype(f + d) type2;  // double
typedef decltype(f < d) type3;  // bool
  • 例子3
template
auto Add(T t, U u) -> decltype(t + u) {
    return t + u;
}

文章作者: Axieyun
版权声明: 本博客所有文章除特別声明外,均采用 CC BY 4.0 许可协议。转载请注明来源 Axieyun !
评论
评论
  目录