我在读关于违反评估顺序的书,他们举了一个让我困惑的例子。
1)如果一个标量对象上的副作用相对于同一标量对象上的另一个副作用未排序,则该行为是未定义的。
/ /剪断
F (i = -1, i = -1);//未定义的行为
在这个上下文中,i是一个标量对象,这显然意味着
算术类型(3.9.1)、枚举类型、指针类型、指向成员类型的指针(3.9.2)、std::nullptr_t以及这些类型的cv限定版本(3.9.3)统称为标量类型。
在这种情况下,我看不出这个说法有什么不明确的地方。在我看来,无论第一个参数还是第二个参数先求值,i最终都是-1,而且两个参数也是-1。
有人能澄清一下吗?
更新
非常感谢大家的讨论。到目前为止,我非常喜欢@harmic的回答,因为它暴露了定义这个语句的陷阱和复杂性,尽管乍一看它是多么简单。@acheong87指出了使用引用时出现的一些问题,但我认为这与这个问题的未排序副作用方面是正交的。
总结
由于这个问题得到了大量的关注,我将总结要点/答案。首先,请允许我稍微离题一点,指出“为什么”可以有密切相关但微妙不同的含义,即“为了什么原因”,“为了什么原因”和“为了什么目的”。我将根据他们所说的“为什么”的意思来对答案进行分组。
为什么?
这里的主要答案来自Paul Draper, Martin J也给出了类似但不那么广泛的答案。保罗·德雷珀的答案可以归结为
它是未定义的行为,因为它没有定义行为是什么。
就解释c++标准的含义而言,这个答案总体上是非常好的。同时还讨论了一些UB的相关情况,如f(++i, ++i);f(i=1, i=-1);在第一个相关的情况下,不清楚第一个参数是否应该是i+1,第二个参数应该是i+2,反之亦然;在第二种情况下,不清楚函数调用后我应该是1还是-1。这两种情况都是UB,因为它们符合以下规则:
如果一个标量对象上的副作用相对于同一标量对象上的另一个副作用没有排序,则该行为是未定义的。
因此,f(i=-1, i=-1)也是UB,因为它属于相同的规则,尽管程序员的意图是(恕我直言)明显和明确的。
Paul Draper在他的结论中也明确指出
它可以被定义为行为吗?是的。它被定义了吗?不。
这给我们带来了一个问题:“f(i=-1, i=-1)出于什么原因/目的被保留为未定义的行为?”
为了什么原因/目的
尽管c++标准中存在一些疏忽(可能是粗心大意),但许多遗漏都是合理的,并为特定的目的服务。虽然我知道这样做的目的通常是“让编译器-作者的工作更容易”,或者“更快的代码”,但我主要感兴趣的是,是否有一个很好的理由把f(I =-1, I =-1)作为UB。
harmic and supercat provide the main answers that provide a reason for the UB. Harmic points out that an optimizing compiler that might break up the ostensibly atomic assignment operations into multiple machine instructions, and that it might further interleave those instructions for optimal speed. This could lead to some very surprising results: i ends up as -2 in his scenario! Thus, harmic demonstrates how assigning the same value to a variable more than once can have ill effects if the operations are unsequenced.
Supercat提供了一个相关的陷阱,试图让f(i=-1, i=-1)做它应该做的事情。他指出,在某些架构上,对同一内存地址的多个同时写入有硬性限制。如果我们处理的是比f(i=-1, i=-1)更简单的东西,编译器可能很难捕捉到这个。
Davidf还提供了一个与harmic非常相似的交叉指令示例。
虽然harmic、supercat和davidf的例子都有些做作,但综合起来,它们仍然为f(i=-1, i=-1)应该是未定义行为提供了一个切实的理由。
我接受了harmic的回答,因为它在解释为什么的所有含义方面做得最好,尽管Paul Draper的回答更好地回答了“原因是什么”这部分。
其他答案
JohnB指出,如果我们考虑重载赋值操作符(而不是简单的标量),那么我们也会遇到麻烦。
行为通常被指定为未定义,如果有一些可以想象的原因,为什么一个试图“有帮助”的编译器可能会做一些完全意想不到的行为。
In the case where a variable is written multiple times with nothing to ensure that the writes happen at distinct times, some kinds of hardware might allow multiple "store" operations to be performed simultaneously to different addresses using a dual-port memory. However, some dual-port memories expressly forbid the scenario where two stores hit the same address simultaneously, regardless of whether or not the values written match. If a compiler for such a machine notices two unsequenced attempts to write the same variable, it might either refuse to compile or ensure that the two writes cannot get scheduled simultaneously. But if one or both of the accesses is via a pointer or reference, the compiler might not always be able to tell whether both writes might hit the same storage location. In that case, it might schedule the writes simultaneously, causing a hardware trap on the access attempt.
当然,有人可能会在这样的平台上实现C编译器,但这并不意味着在硬件平台上使用小到可以被原子处理的类型存储时不应该定义这种行为。试图以无序的方式存储两个不同的值可能会导致奇怪,如果编译器没有意识到它;例如,给定:
uint8_t v; // Global
void hey(uint8_t *p)
{
moo(v=5, (*p)=6);
zoo(v);
zoo(v);
}
if the compiler in-lines the call to "moo" and can tell it doesn't modify
"v", it might store a 5 to v, then store a 6 to *p, then pass 5 to "zoo",
and then pass the contents of v to "zoo". If "zoo" doesn't modify "v",
there should be no way the two calls should be passed different values,
but that could easily happen anyway. On the other hand, in cases where
both stores would write the same value, such weirdness could not occur and
there would on most platforms be no sensible reason for an implementation
to do anything weird. Unfortunately, some compiler writers don't need any
excuse for silly behaviors beyond "because the Standard allows it", so even
those cases aren't safe.
行为通常被指定为未定义,如果有一些可以想象的原因,为什么一个试图“有帮助”的编译器可能会做一些完全意想不到的行为。
In the case where a variable is written multiple times with nothing to ensure that the writes happen at distinct times, some kinds of hardware might allow multiple "store" operations to be performed simultaneously to different addresses using a dual-port memory. However, some dual-port memories expressly forbid the scenario where two stores hit the same address simultaneously, regardless of whether or not the values written match. If a compiler for such a machine notices two unsequenced attempts to write the same variable, it might either refuse to compile or ensure that the two writes cannot get scheduled simultaneously. But if one or both of the accesses is via a pointer or reference, the compiler might not always be able to tell whether both writes might hit the same storage location. In that case, it might schedule the writes simultaneously, causing a hardware trap on the access attempt.
当然,有人可能会在这样的平台上实现C编译器,但这并不意味着在硬件平台上使用小到可以被原子处理的类型存储时不应该定义这种行为。试图以无序的方式存储两个不同的值可能会导致奇怪,如果编译器没有意识到它;例如,给定:
uint8_t v; // Global
void hey(uint8_t *p)
{
moo(v=5, (*p)=6);
zoo(v);
zoo(v);
}
if the compiler in-lines the call to "moo" and can tell it doesn't modify
"v", it might store a 5 to v, then store a 6 to *p, then pass 5 to "zoo",
and then pass the contents of v to "zoo". If "zoo" doesn't modify "v",
there should be no way the two calls should be passed different values,
but that could easily happen anyway. On the other hand, in cases where
both stores would write the same value, such weirdness could not occur and
there would on most platforms be no sensible reason for an implementation
to do anything weird. Unfortunately, some compiler writers don't need any
excuse for silly behaviors beyond "because the Standard allows it", so even
those cases aren't safe.