Skip to content

第三章将亡值解释错误 #68

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

Closed
visvlee opened this issue Jun 4, 2019 · 1 comment
Closed

第三章将亡值解释错误 #68

visvlee opened this issue Jun 4, 2019 · 1 comment
Labels

Comments

@visvlee
Copy link

visvlee commented Jun 4, 2019

实际描述

  • 文件路径:例如,book/zh-cn/02-usability.md
  • 原文段落:
将亡值(xvalue, expiring value),是 C++11 为了引入右值引用而提出的概念(因此在传统 C++中,纯右值和右值是同一个概念),也就是即将被销毁、却能够被移动的值。

将亡值可能稍有些难以理解,我们来看这样的代码:
std::vector<int> foo() {
    std::vector<int> temp = {1, 2, 3, 4};
    return temp;
}

std::vector<int> v = foo();

原文解释如下
http://naipc.uchicago.edu/2015/ref/cppreference/en/cpp/language/value_category.html
A function call expression, if the function's return type is an rvalue reference to object type, such as std::move(val)
A cast expression to an rvalue reference to object type, such as static_cast<T&&>(val) or (T&&)val
a non-static class member access expression, in which the object expression is an xvalue
A pointer-to-member expression in which the first operand is an xvalue and the second operand is a pointer to data member.

将亡值可能稍有些难以理解,我们来看这样的代码:
std::vector<int> foo() {
    std::vector<int> temp = {1, 2, 3, 4};
    return std::move(temp);
}

std::vector<int> v = foo();

附图

必要时,请附上相关截图

@changkun
Copy link
Owner

changkun commented Jun 4, 2019

文中此段的描述确实对读者具备一定迷惑性,因为所提供的代码片段中只是在描述对 xvalue 进行分类的动机,并非介绍 xvalue 自身是什么,也没有错误的说明 foo 返回得到的是 xvalue。

相反,真正的 xvalue 是在接下来一小节“右值引用和左值引用”中的代码片段进行介绍的。

欢迎 PR 改进此段内容的描述。

@changkun changkun added the bug label Jun 4, 2019
HarrisonDing pushed a commit to HarrisonDing/modern-cpp-tutorial that referenced this issue Feb 8, 2020
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