Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

右值引用举例错误 #67

Closed
visvlee opened this issue Jun 4, 2019 · 2 comments
Closed

右值引用举例错误 #67

visvlee opened this issue Jun 4, 2019 · 2 comments
Labels

Comments

@visvlee
Copy link

visvlee commented Jun 4, 2019

实际描述

  • 文件路径:例如,book/zh-cn/02-usability.md
  • 原文段落:
// 防止编译器优化
A return_rvalue(bool test) {
    A a,b;
    if(test) return a;
    else return b;
}

预期描述

a,b是左值,不是右值,上面的代码会调用拷贝构造函数,不是移动构造函数,如果
要掉用移动构造,需要调用std::move()将左值转成右值

// 防止编译器优化
A return_rvalue(bool test) {
    A a,b;
    if(test) return std::move(a);
    else return std::move(b);
}

附图

必要时,请附上相关截图

@changkun changkun closed this as completed Jun 4, 2019
@changkun changkun reopened this Jun 4, 2019
@changkun changkun added question and removed bug labels Jun 5, 2019
@changkun
Copy link
Owner

changkun commented Jun 5, 2019

你好,此处为隐式右值转换,等价于 static_cast<A&&>(a),参见
http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2002/n1377.htm#Moving%20from%20local%20values

补充相关说明:5c37806

@changkun changkun closed this as completed Jun 5, 2019
@visvlee
Copy link
Author

visvlee commented Jun 10, 2019

感谢你的解答

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants