[英]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 butg
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.