C ++ 17后增量操作 [英] C++17 post increment operation

查看:56
本文介绍了C ++ 17后增量操作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

C ++ 17

有人可以解释

int number{5};
number = (number++) + 10;

给出15条输出,

int number {5};
number = (++number) + 10;

给出16的输出?

推荐答案

采用了P0145 (在C ++ 17中),第一个示例的行为不确定。可能发生任何事情。

Before P0145 was adopted (in C++17), the first example had undefined behaviour. Anything could happen.

在C ++ 11之前,两者都有不确定的行为。

Before C++11, both had undefined behaviour.

在C ++ 17中,都没有不确定的行为

In C++17, neither has undefined behaviour. That doesn't mean it's code you want to be writing.

如果我们理解后缀和前缀增量之间的区别


  • number ++ number 变为6,但表达式的计算结果为5

  • 十加到表达式中

  • 结果(15)存储在 number
  • number++: number becomes 6 but the expression evaluates to 5
  • ten is added to the expression
  • the result (15) is stored in number

  • ++ number number 变为6且表达式的计算结果为6

  • ten被添加到表达式中

  • 结果(16)存储在 number

  • ++number: number becomes 6 and the expression evaluates to 6
  • ten is added to the expression
  • the result (16) is stored in number

这篇关于C ++ 17后增量操作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆