简体   繁体   English

C ++ RVO:何时发生?

[英]C++ RVO: when it happens?

http://coliru.stacked-crooked.com/a/c795a5d2bb91ae32 http://coliru.stacked-crooked.com/a/c795a5d2bb91ae32

#include <iostream>
struct X {
    X(const char *) { std::cout << 1; }
    X(const X &) { std::cout << 2; }
    X(X &&) { std::cout << 3; }
};
X f(X a) {
    return a;
}
X g(const char * b) {
    X c(b);
    return c;
}

int main() {
    f("hello"); // 13
    g("hello"); // 1
}

Is there any difference in the last line of function f(X a) : return a; 有没有在功能上的最后一行有什么区别f(X a)return a; instead of return std::move(a); 而不是return std::move(a); ?

Is it true that function f doesn't have RVO but g has NRVO? 函数f没有RVO但g具有NRVO是真的吗?

Is there any difference in the last line of function f(X a): return a; 函数f(X a)的最后一行是否有任何区别:return a; instead of return std::move(a);? 而不是返回std :: move(a);?

No. a is a local variable of the function, so return a can move from it. a是函数的局部变量,因此return a可以从中移动。

Is it true that function f doesn't have RVO but g has NRVO? 函数f没有RVO但g具有NRVO是真的吗?

Correct. 正确。 Named elision never applies to function parameters; 命名省略永远不适用于功能参数; it only applies to local variables that are not function parameters. 它仅适用于不是函数参数的局部变量。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM