是否“返回值优化”?导致未定义的行为?

发布于 2024-08-22 22:23:11 字数 412 浏览 5 评论 0原文

阅读以下问题的一位回复者指出的这篇维基百科文章:

C++ 复制构造函数、临时对象和复制语义

我遇到了这一行

根据编译器和编译器的设置,生成的程序可能会显示以下任何输出:

这不符合未定义的行为吗?我知道这篇文章说取决于编译器和设置,但我只是想清除这一点。

Reading this Wikipedia article pointed by one of the repliers to the following question:

C++ Copy constructor, temporaries and copy semantics

I came across this line

Depending on the compiler, and the compiler's settings, the resulting program may display any of the following outputs:

Doesn't this qualify for undefined behavior? I know the article says Depending on the compiler and settings but I just want to clear this.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

假面具 2024-08-29 22:23:12

不,这不是未定义的行为。未定义的行为在标准中有一个具体的定义(主要是:“诸如在使用错误的程序构造或错误的数据时可能出现的行为,本国际标准对此没有强加要求。”)在这种情况下,行为是未指定的,但不是未定义的。

不同之处在于,任何具有未定义行为的执行都会使程序的所有行为都未定义(即任何事情都可能发生)。对于这种特定的未指定行为,只会发生以下两种情况之一:复制构造函数要么执行,要么不执行。

No, it's not undefined behavior. Undefined behavior has a specific definition in the standard (mostly: "behavior, such as might arise upon use of an erroneous program construct or erroneous data, for which this International Standard imposes no requirements.") In this case, the behavior is unspecified, but not undefined.

The difference is that any execution of anything with undefined behavior makes all the behavior of your program undefined (i.e. anything can happen). With this particular unspecified behavior, only one of two things can happen: either the copy constructor executes, or it doesn't.

且行且努力 2024-08-29 22:23:12

否。该行为被定义为列表中的输出之一。未定义的行为包括恶魔从你的鼻子里飞出来。

请参阅:鼻恶魔

No. The behaviour is defined to be one of the outputs on the list. Undefined behaviour includes demons flying out of your nose.

See: Nasal Demons

乜一 2024-08-29 22:23:12

未定义行为实现定义的行为,这就是这里涉及的内容。

undefined behavior is quite different from implementation defined behavior, which is what's involved here.

岁月蹉跎了容颜 2024-08-29 22:23:12

取决于你所说的未定义是什么意思。我相信其他人在这里所说的 - 根据标准文档使用的定义。但我也知道,当有人说“要么这个或那个,我不会告诉你哪个”时,我认为这是未定义的行为。

不过,这不是什么大问题,因为它永远不会导致错误。当您定义某些方法时,您应该遵循特定的约定来定义它们 - 这是您、编译器以及将使用和维护您的代码的人员之间的一种隐式契约。

在这种情况下,无论您获得复制构造等还是优化行为,效果预计都是相同的 - 调用者收到想要的值。如果您的复制构造函数正在打印“Hello World!”或者有其他不适当的副作用,它没有实现构造函数的预期行为,因此违反合同的错是你的。

Depends what you mean by undefined. I believe what others have said here - by the definition the standards document use. But I also know that when someone says "either this or that, I'm not telling you which" I think of it as undefined behaviour.

It's not a big deal, though, as it should never cause an error. When you define certain methods, you are expected to define them following particular conventions - it's a kind of implicit contract between you, the compiler and the people who will use and maintain your code.

In this case, whether you get a copy-construct etc or the optimised behaviour, the effect is expected to be the same - the caller receives the wanted value. If your copy constructor is printing "Hello World!" or has other inappropriate side-effects, it isn't implementing the expected behaviour for a constructor, so the fault is yours for breaking the contract.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文