- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
返回值优化机制的简短(可能过时且过于简单)总结如下 this :
an implementation may create a hidden object in the caller's stack frame, and pass the address of this object to the function. The function's return value is then copied into the hidden object (...) Around 1991, Walter Bright invented a technique to minimize copying, effectively replacing the hidden object and the named object inside the function with the object used for holding the result [1]
由于这是一个在 SO 上被广泛讨论的话题,我将只链接最完整的 QA我发现。
我的问题是,为什么不总是应用返回值优化?更具体地说(根据 [1]
中的定义)为什么这种替换不总是发生每次函数调用,因为函数返回类型(因此堆栈大小)总是在编译时已知,这似乎是一个非常有用的特性。
最佳答案
显然,当按值返回左值时,没有办法不进行复制。所以,让我们只考虑局部变量。应用于局部变量的一个简单原因是通常不清楚要返回哪个对象。考虑这样的代码:
T f(Args... args) {
T v1{some_init(args)};
T v2{some_other(args)};
bool rc = determine_result(v1, v2);
return rc? v1: v2;
}
在创建局部变量 v1
和 v2
时,编译器无法判断将返回哪个,因此可以就地创建。
另一个原因是复制/移动构建和销毁可能会产生有意的副作用。因此,希望有方法来抑制复制省略。在引入复制省略时,已经有很多 C++ 代码可能依赖于要制作的某些拷贝,即只有少数情况符合复制省略的条件。
关于c++ - 为什么不总是应用 RVO/NRVO?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27709720/
考虑以下 C++ >=17 中的示例代码: struct A{ A() = default; A(const A&) = delete; }; const A f(){ return
如何在最后 3 个运算符中强制执行 RVO: #include class Noisy { private: int m_value; public:
我花了一点时间试图了解 RVO 性能影响是否和我想象的一样有值(value)。 这是我的基准代码(主要思想是创建大结构并从函数返回它们): #include #include #include
我不确定我是否应该打扰,因为它不是任何安全关键应用程序,但我只是好奇并且自己无法弄清楚:编译器会为以下方法应用 RVO 吗? QJsonObject MyClass::buildObject(cons
似乎有很多关于编译器何时执行 RVO 或为什么在某某情况下不执行 RVO 的问题。 看完http://cpp-next.com/archive/2009/08/want-speed-pass-by-v
它们是否跨不同的目标文件工作?它们跨不同的 DLL 工作吗? 我知道这取决于编译器。我很好奇是否有任何编译器和优化设置可以使这项工作正常进行。 最佳答案 通常,是的,但原则上,使用链接时间优化(-fl
在过去一天左右的时间里,我一直在学习移动构造函数,试图坚持大多数人似乎建议的按值(value)返回的一般规则,并且遇到了一个有趣的(对我而言)困境。 假设我有一个昂贵的构造/复制类“C”,它具有正确定
这里有很多关于何时可以完成 RVO 的讨论,但关于何时真正完成的讨论并不多。正如多次声明的那样,RVO 不能根据标准得到保证,但是有没有办法保证 RVO 优化成功或相应代码编译失败? 到目前为止,我部
为什么右值优化不会发生在具有通用引用参数的构造函数的类中? http://coliru.stacked-crooked.com/a/672f10c129fe29a0 #include templa
在 AIX 上使用 xlC 编译以下代码会生成打印“2 2”的代码。在带有 gcc 和 clang 的 Linux 上,它可靠地生成“3 3”。 #include struct Numbers {
我有一个表示二进制消息的结构。我想编写一个函数来从缓冲区(无论是文件还是套接字,都无所谓)中获取下一条这样的记录: template Record getNext(); 现在,我可以这样写: tem
http://coliru.stacked-crooked.com/a/c795a5d2bb91ae32 #include struct X { X(const char *) { std:
考虑以下几点: struct A { /* ... */ }; A foo() { auto p = std::make_pair(A{}, 2); // ... do something
在 [C++11: 12.8/31] 中有说明: This elision of copy/move operations, called copy elision, is permitted [..
我有整个故事,关于我发现我从函数返回的无序映射实际上不是 RVO 的令人沮丧的旅程,尽管我确信它在更早的时候是无关紧要的。 有没有办法检查 RVO 是否在任何给定函数中发生?或者像一个做什么和不做什么
在许多情况下,我想创建一个新的数据实例并将其返回给 API 调用方。 我了解到 unique_ptr/shared_ptr 可用于工厂模式(例如, Factory pattern using uniq
假设我写了: Foo get_a_foo() { return reinterpret_cast(get_a_bar()); } 并假设 sizeof(Foo) == sizeof(Bar)。
“返回值优化”的概念是否适用于 C++ 编译器中的 lambda 表达式?我知道这取决于编译器和优化参数,但理论上可能吗? 顺便说一句,有人知道 VS.NET 2013 或更高版本中的这个问题吗? 最
返回值优化机制的简短(可能过时且过于简单)总结如下 this : an implementation may create a hidden object in the caller's stack
我刚刚了解了 RVO(返回值优化)和 NRVO(命名返回值优化)。下面是两个例子 //Example of RVO Bar Foo() { return Bar(); } //Example
我是一名优秀的程序员,十分优秀!