首页 > 编程知识 正文

c 11新特性,给出i+i+i的语法树

时间:2023-05-05 17:11:56 阅读:30003 作者:4262

在某些情况下,不需要或不能定义变量,但想要某种类型。 此时,可以使用C 11提供的decltype关键字。 在编译器编译时起到推导表达式类型的作用。

语法:

decltype (表达式) decltype是' declare type '的缩写,表示“声明类型”。 decltype的导出是在编译时进行的,只是用于导出表达式类型,而不计算表达式的值。

代码为以下:

int a=10; 戴尔类型(a ) b=99; //b-intdecltype(a3.14 ) c=52.13; //c-doubledecltype(aB*c ) d=520.1314; //d - double推导规则:

1 .表达式是一般变量、一般表达式或类表达式,在这种情况下,使用decltype派生的类型与表达式的类型匹配。

代码为以下:

# includeiostreamusingnamespacestd; 类测试{ public : int num=9; 字符串文本; 静态常数输入值=110; (; int main () {int x=99; 常数int y=x; 戴尔类型(x ) a=x; //a-intdecltype(y ) b=x; //B- constintdecltype (test :3360 value ) c=0; //c - const int Test t; DECLtype(t.text ) d='hello world '; //d - string} 2.表达式是函数调用,使用decltype导出的类型与函数的返回值相匹配。

注意:

函数func_cint ) )返回纯右值。 对于纯右值,派生的变量d的类型为int,而不是const int,因为只有类类型具有const,volatile修饰符,否则必须忽略这两个修饰符。

代码为以下:

# includeiostreamusingnamespacestd; class Test{}; intfunc_int({}intfunc_int_r ) {}int func_int_rr ) {}const int func_cint ) { } const int func _ cint } //a-intDecltype(func_int_r ) ) b=n; //b-intDecltype(func_int_RR ) ) c=0; //c-intDecltype(func_cint ) ) d=0; /*d -int纯右值表示派生的变量d的类型是int,而不是const int,因为只有类类型具有const,volatile修饰符,否则必须忽略这两个修饰符。 */Decltype(func_cint_r ) ) e=n; //e-constintdecltype (func _ cint _ RR ) ) f=0; //f-constintdecltype (func _ CT est () ) g=Test ); //g - const Test} 3.表达式用左值或括号()括起来,使用decltype派生的是对表达式类型的引用(如果存在const,则volatile修饰符不可忽略)。

代码为以下:

# includeiostreamusingnamespacestd; 类测试{ public : int num=9; 字符串文本; 静态常数输入值=110; (; int main () {const Test obj; DECLtype(obj.num ) a=3; //a-intdecltype () obj.num ) ) b=a; //b -const int int n=0,m=0; 戴尔类型(nm ) c=0; //c-intdecltype(n=nm ) d=n//d - int n=n m,最终得到的n是左边的值}

版权声明:该文观点仅代表作者本人。处理文章:请发送邮件至 三1五14八八95#扣扣.com 举报,一经查实,本站将立刻删除。