[C++11] decltype的使用

wgxabc 2016-08-30 06:35:26
int i =4;
decltype(++i) var9 = i; //var9:int& ++i返回i的左值
decltype(i++) var10; //var10:int i++返回右值

问题:
为什么上面的++i和i++返回的不一样呢?
...全文
136 7 打赏 收藏 转发到动态 举报
写回复
用AI写文章
7 条回复
切换为时间正序
请发表友善的回复…
发表回复
paschen 版主 2016-08-30
  • 打赏
  • 举报
回复
对于 decltype b) if the value category of expression is lvalue, then decltype yields T&; http://en.cppreference.com/w/cpp/language/decltype
paschen 版主 2016-08-30
  • 打赏
  • 举报
回复
前者返回的是左值 后者返回的是右值
ztenv 版主 2016-08-30
  • 打赏
  • 举报
回复
++i,返回的是自身加一的结果, i++,返回的是自身加一之前的临时值,
赵4老师 2016-08-30
  • 打赏
  • 举报
回复
fefe82 2016-08-30
  • 打赏
  • 举报
回复
续1楼: 4 For an expression e, the type denoted by decltype(e) is defined as follows: (4.1) — if e is an unparenthesized id-expression or an unparenthesized class member access (5.2.5), decltype(e) is the type of the entity named by e. If there is no such entity, or if e names a set of overloaded functions, the program is ill-formed; (4.2) — otherwise, if e is an xvalue, decltype(e) is T&&, where T is the type of e; (4.3) — otherwise, if e is an lvalue, decltype(e) is T&, where T is the type of e; (4.4) — otherwise, decltype(e) is the type of e. decltype(++i) var9 = i; //var9:int& ++i返回i的左值, (4.3) decltype(i++) var10; //var10:int i++返回右值, (4.4) , prvalue 不是 xvalue 。
ri_aje 2016-08-30
  • 打赏
  • 举报
回复
前加返回左值,后加返回右值。 decltype 对左值的结果就是引用,对右值就是非引用类型。
pengzhixi 2016-08-30
  • 打赏
  • 举报
回复
The operand of prefix ++ is modified by adding 1, or set to true if it is bool (this use is deprecated). The operand shall be a modifiable lvalue. The type of the operand shall be an arithmetic type or a pointer to a completely-defined object type. The result is the updated operand; it is an lvalue. The value of a postfix ++ expression is the value of its operand. [Note: the value obtained is a copy of the original value —end note ] The operand shall be a modifiable lvalue. The type of the operand shall be an arithmetic type or a pointer to a complete object type. The value of the operand object is modified by adding 1 to it, unless the object is of type bool, in which case it is set to true. [Note: this use is deprecated, see Annex D. —end note ] The value computation of the ++ expression is sequenced before the modification of the operand object. With respect to an indeterminately-sequenced function call, the operation of postfix ++ is a single evaluation. [Note: Therefore, a function call shall not intervene between the lvalue-to-rvalue conversion and the side effect associated with any single postfix ++ operator. —end note ] The result is a prvalue

64,685

社区成员

发帖
与我相关
我的任务
社区描述
C++ 语言相关问题讨论,技术干货分享,前沿动态等
c++ 技术论坛(原bbs)
社区管理员
  • C++ 语言社区
  • encoderlee
  • paschen
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
  1. 请不要发布与C++技术无关的贴子
  2. 请不要发布与技术无关的招聘、广告的帖子
  3. 请尽可能的描述清楚你的问题,如果涉及到代码请尽可能的格式化一下

试试用AI创作助手写篇文章吧