【问题标题】:Why there is output difference for give code in c++ 11 and c++ 17?为什么在 c++ 11 和 c++ 17 中给出代码存在输出差异?
【发布时间】:2020-04-16 19:01:41
【问题描述】:

我有一段代码

     #include <bits/stdc++.h>
      using namespace std;
      typedef long long ll ; 
      int main() 
      { 
             ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0) ; 
             int i, j;
             i = j = 5;
             j = i++ + (i*10);
             cout<<i<<" "<< j <<" "<<i++;

    }

c++ 17 的代码输出为:6 65 6 c++ 11 的代码输出为:7 65 6

为什么会有这样的差异?

【问题讨论】:

标签: c++ c++11 c++17 postfix-operator


【解决方案1】:

在 C++17 之前,表达式 j = i++ + (i*10); 表现出未定义的行为。

在 C++17 中,更改了语言规则以定义此类表达式的行为。 (修改变量并在同一个表达式中使用)

因此,根据您的编译器、您的平台、您使用的优化设置、月相等,您可能会得到相同的答案......或不会。 p>

【讨论】:

  • 你能详细说明 C++17 的变化吗?在我的脑海中,这看起来仍然未定义。
  • ` i++ + (i*10)` afaik 对i 的访问仍未排序。
猜你喜欢
  • 2014-10-08
  • 1970-01-01
  • 2018-02-04
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-09-21
相关资源
最近更新 更多