返回值优化(RVO)不是一个错误吗?

发布于 2024-09-27 02:50:50 字数 493 浏览 0 评论 0原文

我可能问了一个愚蠢的问题,但我在此处查看了 RVO 的维基百科页面,并且无法停止想知道这种行为是否错误。我在我的机器上尝试过,尽管有优化级别,但 RVO 已完全启动。如果构造函数中确实发生了的事情怎么办?我知道不应该,但如果呢?我不明白为什么当构造函数中有副作用时 RVO 仍然会发生。

编辑:-fno-elide-constructors 似乎停止了 RVO。但问题仍然存在。

EDIT2:更严肃地说,有多少人知道这样的事情?它可能在标准中,但在我看来它仍然是一个非常丑陋的功能。至少编译器应该默认禁用它,并为了解这一点的人提供一个开关。 :)

编辑 3:我仍然坚持认为这真的很糟糕。 :)。我不认为我知道任何其他像这样直接违背语言语法的语言约束。其他一切都会引发编译器或链接器错误,对吧?

I maybe asking a dumb question, but I looked at the wikipedia page for RVO here and could not stop wondering if that behavior is wrong. I tried it in my machine and RVO is fully kicked in despite optimization level. What if there was actually something BIG happenning in a constructor? I know it shouldn't, but what if? I can't understand why RVO would still happen when there are side effects in the constructor.

EDIT: -fno-elide-constructors seems to stop RVO. But the question remains.

EDIT2: On a more serious note, how many people know about something like this? It maybe in the standard, but it is still a really ugly feature as I see it. At least compilers should disable it by default and provide a switch for people who know about this. :)

EDIT 3: I still insist that this is really bad. :). I don't think I know of any other language constraint like this that goes directly against the syntax of language. Everything else throws either compiler or linker errors right?

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

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

发布评论

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

评论(3

∞觅青森が 2024-10-04 02:50:50

该标准规定,与程序的可观察状态有关的操作不得被优化掉,除了复制构造某些情况。您不能依赖复制构造函数来执行,即使它们具有您期望看到的副作用(例如,控制台输出)。

The standard mandates that operations with concern a program's observable state must not be optimized away, except for copy construction in certain circumstances. You must not rely on copy constructors to be executed, even if they have side effects you expect to see (e.g., console output).

彩虹直至黑白 2024-10-04 02:50:50

正如其他答案中所述,编译器甚至可以优化不平凡的复制构造函数和赋值运算符。

15.8.12

当满足某些条件时,允许实现省略类对象的复制构造,即使
对象的复制构造函数和/或析构函数有副作用
。在这种情况下,实施将处理
省略的复制操作的源和目标只是引用同一对象的两种不同方式,并且
该对象的销毁发生在两个对象在没有该对象的情况下会被销毁的较晚时间
优化。在以下情况下允许省略复制操作(可以组合使用)
消除多个副本):

——在具有类返回类型的函数的 return 语句中,当表达式是非易失性的名称时
具有与函数返回类型相同的 cv-unqualified 类型的自动对象,可以省略复制操作
通过直接将自动对象构造到函数的返回值中

——当一个尚未绑定到引用(12.2)的临时类对象被复制到一个类对象时
相同的cv-unqualified类型,可以通过直接构造临时对象来省略复制操作
省略副本的目标

As said in the other answers, the compiler is allowed optimize away even non trivial copy constructors and assignment operators.

12.8.15

When certain criteria are met, an implementation is allowed to omit the copy construction of a class object, even if
the copy constructor and/or destructor for the object have side effects
. In such cases, the implementation treats the
source and target of the omitted copy operation as simply two different ways of referring to the same object, and the
destruction of that object occurs at the later of the times when the two objects would have been destroyed without the
optimization. This elision of copy operations is permitted in the following circumstances (which may be combined
to eliminate multiple copies):

— in a return statement in a function with a class return type, when the expression is the name of a non-volatile
automatic object with the same cv-unqualified type as the function return type, the copy operation can be omitted
by constructing the automatic object directly into the function’s return value

— when a temporary class object that has not been bound to a reference (12.2) would be copied to a class object with
the same cv-unqualified type, the copy operation can be omitted by constructing the temporary object directly into
the target of the omitted copy

月依秋水 2024-10-04 02:50:50

定义“错误”。 C++ 语言明确允许这种优化,即使它是可观察到的。如果您的程序的行为取决于特定的实现,那么不幸的是您使用的不是 ISO C++,而是某种方言。

Define "wrong". The C++ language explicitly permits this kind of optimization even though it is observable. If the behavior of your program depends on a specific implementation, then unfortunately you are not using ISO C++, but some dialect.

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