简体   繁体   中英

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:

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?

decltype((i));

Will yield a reference type since i is an lvalue. This is useful to determine the value category of any expression. Reproduced form cppreference , for parenthesized expression (emphasis mine):

a. if the value category of expression is xvalue , then decltype yields T&& ;

b. if the value category of expression is lvalue , then decltype yields T& ;

c. if the value category of expression is prvalue , then decltype yields T .


decltype(k+0)

Will yield the type of the result the k+0 expression will evaluate to. Just as auto val = k + 0; would deduce val .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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