gcc 应用程序运行不规律,但 clang 工作正常

gcc app runs irregularly but clang works fine

我写了一个简单的基于堆栈的虚拟机,您可以在 https://github.com/radinParsaei/VM 查看它的源代码 当我用 clang 编译它时它工作得很好但是如果它用字节码中的 GCC 编译我编码像 pop() / pop() 它工作不规则并且它 returns stack[stack.size() - 2] / stack[stack.size() - 1] 我通过编写代码来解决这个问题 Value a = pop(); Value b = pop(); return a / b; 谁有更好的主意?

正如@IlCapitano 所说,您的第二个 pop() 可能会被首先调用:

There is no concept of left-to-right or right-to-left evaluation in C++. This is not to be confused with left-to-right and right-to-left associativity of operators: the expression a() + b() + c() is parsed as (a() + b()) + c() due to left-to-right associativity of operator+, but the function call to c may be evaluated first, last, or between a() or b() at run time

关键: 对 c 的函数调用可能在 运行 时间 时首先、最后或在 a() 或 b() 之间求值。

回复评论:

order of evaluation is unspecified (with some exceptions). The compiler can evaluate operands and other subexpressions in any order, and may choose another order when the same expression is evaluated again.

所以这取决于编译器。每个人的做法都不一样。