有一个非常简单的 UB 例子:
int i = 1;
i = i++; // classic example of UB.
我最近看到,如何使用 Pascal 风格的 inc
操作。 Eric Niebler github
// this structure little difference than original.
struct inc_t
{
template< typename T>
T operator()(T& t ) const { return t++; }
};
constexpr inc_t inc{};
//usage
int i = 1;
inc(i);
//or
int j = inc(i);
所以,合并:
int i = 1;
i = inc(i); // Is there still UB ?
谢谢。
最佳答案
否。
i = i++
是 UB 因为 i
递增时没有指定;它可以在 i++
的值计算之后的任何时候(即,i
的左值到右值的转换>) 和完整表达式结束之前。因此,相对于赋值语句,此写入是无序的。对同一 int
的两次无序写入是未定义的行为。
但在 i = inc(i)
中,i
的递增发生在函数返回之前,因为 t++
中的完整表达式发生在函数内部。反过来,函数必须在右侧的值计算之前返回,并且赋值在两侧的值计算之后排序。因此i
的递增顺序在赋值之前,没有UB。
C++11标准的相关引述:
§1.9/14
Every value computation and side effect associated with a full-expression is sequenced before every value computation and side effect associated with the next full-expression to be evaluated.
§5.17/1
In all cases, the assignment is sequenced after the value computation of the right and left operands, and before the value computation of the assignment expression.
关于c++ - 这里有更多 C++ 中未定义的行为,很抱歉这个问题,但又是 UB,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22076038/