【C++11】decltype关键字使用总结

本文总结了C++11中的decltype关键字,它用于编译时类型推导。与auto不同,decltype根据表达式而非初始值推导类型,并保留引用和cv属性。文章介绍了decltype的基本用法,推导规则,以及与auto配合使用于函数返回值类型的场景。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

decltype是“declare type”的缩写,译为“声明类型”。和 auto 的功能一样,都用来在编译时期进行自动类型推导。

那么,问题来了,既然有了auto关键字,为何C++11还会引入decltype关键字呢,既然引入,那肯定是有区别的。另一方面,因为 auto 并不适用于所有的自动类型推导场景,在某些特殊情况下 auto 用起来非常不方便,甚至压根无法使用,所以 decltype 关键字也被引入到 C++11 中。

 

1。基本用法

首先来看下他们的基本用法:

auto varname = value;
decltype(exp) varname = value;

这里,varname 表示的是变量名,value 表示的是赋给变量的值,exp 表示的是一个表达式。

那么,auto 是根据=右边的初始值 value 推导出变量的类型,而 decltype 则是根据 exp 表达式推导出变量的类型,跟=右边的 value 没有关系。所以,decltype就不再要求变量必须初始化了。

所以也可以这么写:

decltype(exp) varname;

这里有一点必须要注意:必须要保证 exp 的结果是有类型的,不能是 void。

(decltype不会像auto一样忽略引用和cv属性,decltype会保留表达式的引用和cv属性)

 

2。推导规则

decltype的用法可简单,可复杂,也许可以复杂到颠覆你对C++的认知。但不管怎样,decltype的推导总是要遵循以下原则:

  • 如果 exp 是一个不被括号( )包围的表达式,或者是一个类成员访问表达式,或者是一个单独的变量,那么 decltype(exp) 的类型就和 exp 一致,这是最普遍最常见的情况。
  • class AA 
    {
        static int a;
        string     b;
    }
    
    int n = 0;
    const int &r = n;
        
    AA st;
        
    decltype(n) a = n;  //n 为 int 类型,a 被推导为 int 类型
    decltype(r) b = n;  //r 为 const int& 类型, b 被推导为 const int& 类型
    decltype(AA::a) c = 0;//a为类 AA中的一个int类型的成员变量,c被推导为int类型
    decltype(AA.b) url = "http://c.biancheng.net/cplus/";  //b为类AA的一个string类型的成员变量, url被推导为 string 类型

     

  • 如果 exp 是函数调用,那么 decltype(exp) 的类型就和函数返回值的类型一致。
  • int& func_int_r(int, char);  //返回值为 int&
    int&& func_int_rr(void);  //返回值为 int&&
    int func_int(double);  //返回值为 int
    const int& fun_cint_r(int, int, int);  //返回值为 const int&
    const int&& func_cint_rr(void);  //返回值为 const int&&
    
    
    int n = 100;
    decltype(func_int_r(100, 'A')) a = n;  //a 的类型为 int&
    decltype(func_int_rr()) b = 0;  //b 的类型为 int&&
    decltype(func_int(10.5)) c = 0;   //c 的类型为 int
    decltype(fun_cint_r(1,2,3))  x = n;    //x 的类型为 const int &
    decltype(func_cint_rr()) y = 0;  // y 的类型为 const int&&
  • 如果 exp 是一个左值,或者被括号( )包围,那么 decltype(exp) 的类型就是 exp 的引用;假设 exp 的类型为 T,那么 decltype(exp) 的类型就是 T&。
  • class Base{
    public:
        int x;
    };
    
    int main(){
        const Base obj;
    
        //带有括号的表达式
        decltype(obj.x) a = 0;  //obj.x 为类的成员访问表达式,符合推导规则一,a 的类型为 int
        decltype((obj.x)) b = a;  //obj.x 带有括号,符合推导规则三,b 的类型为 int&。
        
        //加法表达式
        int n = 0, m = 0;
        decltype(n + m) c = 0;  //n+m 得到一个右值,符合推导规则一,所以推导结果为 int
        decltype(n = n + m) d = c;  //n=n+m 得到一个左值,符号推导规则三,所以推导结果为 int&
        return 0;
    }

3。auto与decltype 配合使用

      auto和decltype一般配合使用在推导函数返回值的类型问题上。

      返回类型后置的配合使用方法:

template<typename T, typename U>
auto add(T t, U u) -> decltype(t + u) {
    return t + u;
}

     返回值后置类型语法就是为了解决函数返回值类型依赖于参数但却难以确定返回值类型的问题 。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值