- iOS/Objective-C 元类和类别
- objective-c - -1001 错误,当 NSURLSession 通过 httpproxy 和/etc/hosts
- java - 使用网络类获取 url 地址
- ios - 推送通知中不播放声音
当我在 VS2010 中运行此代码时,不应用 NRVO。
#include <stdio.h>
class A
{
public:
A() { printf( "I am in constructor\n" ); }
A(const A& a) { printf( "I am in copy constructor\n" ); }
~A() { printf( "I am in destructor\n" ); }
int i;
};
A f(int j)
{
A a;
if ( j ) return a;
a.i = j;
return a;
}
int main()
{
A a;
a = f(5);
}
编辑:这与析构函数有关。当我注释掉它的行时,使用了 NRVO。但这是为什么呢?
最佳答案
Why NRVO is not applied here?
如果这纯粹是您的好奇心,并且您想知道 VC10 如何通过算法决定是否执行 NRVO,那么唯一能够可靠地回答这个问题的人就是那些了解 VC10 内部工作原理的人 - 那些编写它的人.
据我所知,根据 C++11 标准,编译器被允许在这种情况下执行 NRVO,不这样做只是编译器的决定——不是因为任何有效性约束。根据第 12.8/31 段:
[...] This elision of copy/move operations, called copy elision, 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 (other than a function or catch-clause parameter) with the same cv-unqualified type as the function return type, the copy/move operation can be omitted by constructing the automatic object directly into the function’s return value
[...]
但是,如果您希望能够强制您的编译器执行 NRVO,那么答案是“您不能”。
是否应用 NRVO 完全由编译器决定。你不能指望它,你也不能指望它不被执行。据我所知,这是所谓的“as-if”规则的唯一异常(exception)。
也就是说,随着您提高优化级别,执行 NRVO 的机会也会增加。
关于c++ - 为什么这里不应用 NRVO?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16038511/
我看到了这个问题 When is an object "out of scope"? 我看了sparc_spread 的回答,发现其中有一个问题。在他的回答的这一部分: Circle myFunc (
在程序中如下 #include struct A { A() { std::cout << "0"; } A( const A & ) { std::cout << "1"; }
在以下示例中,根据 this article 应用 NRVO(命名返回值优化): std::string f1() { std::string str; return str; //
#include using namespace std; int gc = 0; struct A { int id; A():id(gc++) { cout 1)。并且符合规范
class Date { private: int day,month,year; public: Date (int d,int m,int y) { day=d; month=m; year=y;
我们有一个广泛使用 out 参数的代码库,因为每个函数都可能因某些错误枚举而失败。这变得非常困惑,代码有时不可读。 我想消除这种模式并引入更现代的方法。 目标是转变: error_t fn(param
当我在 VS2010 中运行此代码时,不应用 NRVO。 #include class A { public: A() { printf( "I am in constructor\
我认为应该这样做,因为它对正确性很重要。但是,我很惊讶地看到 Clang 的输出。考虑以下代码: #include struct S { int i; S(int i) : i(i)
我希望了解 C++ 中 NRVO 的局限性。具有初始命名声明和单个返回的函数是否会针对 NRVO 进行优化并省略 T val,即使函数 myFunc() 具有抛出的可能性? T myFunc(bool
我有以下代码。 #include struct Box { Box() { std::cout << "constructed at " << this << '\n'; }
返回值优化机制的简短(可能过时且过于简单)总结如下 this : an implementation may create a hidden object in the caller's stack
我已经阅读了一些关于 move 函数的帖子(例如 http://www.cprogramming.com/c++11/rvalue-references-and-move-semantics-in-c
我需要调用一个函数为我返回一个对象。问题是对象有一个析构函数,它可以在函数输出被分配给另一个对象之前破坏数据。在我的程序中,我有一个 operator+,它将两个矩阵相加并返回两个矩阵的和: C=A+
我刚刚了解了 RVO(返回值优化)和 NRVO(命名返回值优化)。下面是两个例子 //Example of RVO Bar Foo() { return Bar(); } //Example
如果我编写一个在本地实例化对象然后按值返回的工厂方法,打算利用 NRVO(根据此处的一些答案:c++11 Return value optimization or move?),将指向本地对象的指针/
假设我有这样一个函数: using std::vector; vector build_vector(int n) { if (some_condition(n)) return {};
假设我们有以下代码: std::vector f() { std::vector y; ... return y; } std::vector x = ... x = f(); 似乎编译器
想象一下: S f(S a) { return a; } 为什么不允许别名a和返回值槽? S s = f(t); S s = t; // can't generally transform it
我一直在研究 NRVO 及其对不同编译器的支持,并遇到了奇怪的行为,这相当令人困惑。 示例代码: #include using namespace std; class X
自 1992 年以来我一直在使用 C++(并阅读了大量有关该语言的资料),因此我对这门语言有相当多的了解,但远非全部。我的问题是关于 C++11 命名返回值优化——有什么保证会执行?我倾向于发送非常量
我是一名优秀的程序员,十分优秀!