简体   繁体   English

为什么decltype((i))是引用类型但是decltype(i + 0)是int类型?

[英]Why decltype((i)) is reference type but decltype(i+0) is int type?

I am studying C++ Primer fifth edtion,and the example code really confused me.It is similar as the code below: 我正在学习C ++ Primer第五版,示例代码让我很困惑。它与下面的代码类似:

int i,&k=i;
decltype((i)) t;    //error: t must be initialized
decltype(k+0) s = 45;  //OK,s is int type

Why the two are expressions and the first one is reference type but the second one is int type? 为什么这两个是表达式,第一个是引用类型,但第二个是int类型?

decltype((i));

Will yield a reference type since i is an lvalue. 将产生一个引用类型,因为i是一个左值。 This is useful to determine the value category of any expression. 这对于确定任何表达式的值类别很有用。 Reproduced form cppreference , for parenthesized expression (emphasis mine): 转载形式cppreference ,用于括号表达式(强调我的):

a. 一个。 if the value category of expression is xvalue , then decltype yields T&& ; 如果表达式的值类别是xvalue ,则decltype产生T && ;

b. if the value category of expression is lvalue , then decltype yields T& ; 如果表达式的值类别是左值 ,则decltype产生T& ;

c. C。 if the value category of expression is prvalue , then decltype yields T . 如果表达式的值类别是prvalue ,则decltype产生T.


decltype(k+0)

Will yield the type of the result the k+0 expression will evaluate to. 将产生k+0表达式将评估的结果类型。 Just as auto val = k + 0; 就像auto val = k + 0; would deduce val . 会推断出val

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM