相关疑难解决方法(0)

未定义的行为和序列点

什么是"序列点"?

未定义的行为和序列点之间的关系是什么?

我经常使用有趣和复杂的表达方式a[++i] = i;,让自己感觉更好.我为什么要停止使用它们?

如果您已阅读此内容,请务必访问后续问题重新加载未定义的行为和序列点.

(注意:这是Stack Overflow的C++常见问题解答的一个条目.如果你想批评在这种形式下提供常见问题解答的想法,那么发布所有这些的元数据的发布将是这样做的地方.这个问题在C++聊天室中受到监控,其中FAQ的想法一开始就出现了,所以你的答案很可能被那些提出想法的人阅读.)

c++ c++-faq undefined-behavior sequence-points

970
推荐指数
4
解决办法
10万
查看次数

移位操作数在C++中排序17

我读了C++ 17 Standard $ 8.5.7.4:

表达E1在表达E2之前测序.

换班运营商.

此外cppreference第19条说:

In a shift operator expression E1<<E2 and E1>>E2, every value
computation and side-effect of E1 is sequenced before every value
computation and side effect of E2
Run Code Online (Sandbox Code Playgroud)

但是当我尝试使用gcc 7.3.0或clang 6.0.0编译以下代码时

#include <iostream>
using namespace std;

int main() {
    int i = 5;
    cout << (i++ << i) << endl;
    return 0;
}
Run Code Online (Sandbox Code Playgroud)

我得到以下gcc警告:

../src/Cpp_shift.cpp: In function ‘int main()’:
../src/Cpp_shift.cpp:6:12: warning: operation on ‘i’ may be undefined [-Wsequence-point]
  cout << (i++ << …
Run Code Online (Sandbox Code Playgroud)

c++ sequence-points language-lawyer c++17

13
推荐指数
1
解决办法
397
查看次数

在cout语句中首先运行什么?(C++ 17)

比方说,我有一个很长的声明

cout << findCurrent() << "," << findLowest() << "," << findHighest() << "," << findThird()<<"\n";

会像逻辑指令一样findCurrent()运行findLowest()吗?

c++ cout operator-overloading operator-precedence c++17

1
推荐指数
1
解决办法
151
查看次数