自动类型推断 decltype类型推导

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

 

为什么需要 decltype 关键字

auto并不适用于所有的自动类型推导。

必须对auto声明的类型进行初始化,否则编译器不能推导出auto的实际类型。而有些场景需要根据运行结果进行类型推导。这是auto不能做到的。

auto 只能用于类的静态成员,不能用于类的非静态成员(普通成员),如果我们想推导非静态成员的类型,这个时候就必须使用 decltype  。

格式

auto varname = value;

decltype(exp) varname = value;

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

decltype( f() ) sum = x;   

sum的类型就是函数f的返回值类型。

 

decltype 不要求必须初始化

auto 根据“=”右边的初始值 value 推导出变量的类型,不初始化变量的类型就无法推导。

而 decltype 根据 exp 表达式推导出变量的类型,跟=右边的 value 没有关系。

decltype 基本用法举例 

int a = 0;

decltype(a) b = 1;         //b 被推导成 int

decltype(10.8) x = 5.5;  //x 被推导成 double

decltype(x + 100) y;      //y 被推导成 double,注意:y 没有被初始化。

 

decltype 的详细用法

auto 的语法格式比 decltype 简单,在一般的类型推导中,使用 auto更加方便。

decltype 的用法非常复杂。当程序员使用 decltype(exp) 获取类型时,编译器将根据以下三条规则得出结果:

 

1、如果 exp 是一个不被括号( )包围的表达式,或者是一个类成员访问表达式,或者是一个单独的变量,那么 decltype(exp) 的类型就和 exp 一致,这是最普遍最常见的情况。

例1:exp 是一个普通表达式

class Student{

public:

    static int total;

    string name;

};

int Student::total = 0;

 

int  main(){

    int n = 0;

    const int &r = n;

    decltype(n) a = n;  //n 为 int 类型,a 被推导为 int 类型

    decltype(r) b = n;     //r 为 const int& 类型, b 被推导为 const int& 类型

    decltype(Student::total) c = 0;  //total 为类 Student 的一个 int 类型的成员变量,c 被推导为 int 类型

    Student stu;

    decltype(stu.name) url = "http://c.biancheng.net/cplus/";  //total 为类 Student 的一个 string 类型的成员变量, url 被推导为 string 类型

    return 0;

}

 

2、如果 exp 是函数调用,那么 decltype(exp) 的类型就和函数返回值的类型一致。

例2: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&&

//decltype类型推导

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 中调用函数时需要带上括号和参数,但这仅仅是形式,并不会真的去执行函数代码。

 

3、这点特殊,如果 exp 是一个左值,或者被括号( )包围,那么 decltype(exp) 的类型就是 exp 的引用;假设 exp 的类型为 T,那么 decltype(exp) 的类型就是 T&。

例3:特殊用法,exp 是左值,或者被( )包围:

class CBase{

public:

    int x;

};

int main(){

    const CBase  obj;

    //带有括号的表达式

    decltype(obj.x)  a = 0;       //obj.x 为类的成员访问表达式,符合推导规则一,a 的类型为 int

    decltype( (obj.x) ) b = a;  //obj.x 带有括号,符合推导规则三,b 的类型为 int&

    //加法表达式

    int j = 0, k = 0;

    decltype(j + k) a = 0;        //j+k 得到一个右值,符合推导规则一,所以推导结果为 int

    decltype(a = j + k) b = c;  //a=j+k 得到一个左值,符号推导规则三,所以推导结果为 int&

    return 0;

}

左值和右值:

左值是指那些在表达式执行结束后依然存在的数据,也就是持久性的数据;

右值是指那些在表达式执行结束后不再存在的数据,也就是临时性的数据。

有一种很简单的方法来区分左值和右值,对表达式取地址,如果编译器不报错就为左值,否则为右值。

 

模板追踪返回类型

decltype用法:

申明函数的返回类型,一种新的指定函数返回类型的方式

template<typename T1, typename T2>

auto Add(T1 x, T2 y) ->decltype(x + y);

template<typename T1, typename T2>

auto  Add(T1& t1, T2& t2) -> decltype(t1 + t2){

        return t1 + t2;

}

复合符号->decltype(t1 + t2)被称为追踪返回类型,函数返回值的位置由auto关键字占据。 

如果没有返回类型,可以声明为void 。

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值