Skip to content

Commit 63164b5

Browse files
committed
fix changkun#68: more detail to the example of xvalue
1 parent 5c37806 commit 63164b5

File tree

1 file changed

+2
-4
lines changed

1 file changed

+2
-4
lines changed

book/zh-cn/03-runtime.md

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -210,11 +210,9 @@ std::vector<int> foo() {
210210
std::vector<int> v = foo();
211211
```
212212

213-
在这样的代码中,函数 `foo` 的返回值 `temp` 在内部创建然后被赋值给 `v`,然而 `v` 获得这个对象时,会将整个 temp 拷贝一份,然后把 `temp` 销毁,如果这个 `temp` 非常大,这将造成大量额外的开销(这也就是传统 C++ 一直被诟病的问题)。在最后一行中,`v` 是左值、`foo()` 返回的值就是右值(也是纯右值)。
213+
在这样的代码中,就传统的理解而言,函数 `foo` 的返回值 `temp` 在内部创建然后被赋值给 `v`,然而 `v` 获得这个对象时,会将整个 temp 拷贝一份,然后把 `temp` 销毁,如果这个 `temp` 非常大,这将造成大量额外的开销(这也就是传统 C++ 一直被诟病的问题)。在最后一行中,`v` 是左值、`foo()` 返回的值就是右值(也是纯右值)。但是,`v` 可以被别的变量捕获到,而 `foo()` 产生的那个返回值作为一个临时值,一旦被 `v` 复制后,将立即被销毁,无法获取、也不能修改。而将亡值就定义了这样一种行为:临时的值能够被识别、同时又能够被移动
214214

215-
但是,`v` 可以被别的变量捕获到,而 `foo()` 产生的那个返回值作为一个临时值,一旦被 `v` 复制后,将立即被销毁,无法获取、也不能修改。
216-
217-
将亡值就定义了这样一种行为:临时的值能够被识别、同时又能够被移动。
215+
在 C++11 之后,编译器为我们做了一些工作,此处的左值 `temp` 会被进行此隐式右值转换,等价于 `static_cast<std::vector<int> &&>(temp)`,进而此处的 v 会将 `foo` 局部返回的值进行移动。也就是后面我们将会提到的移动语义。
218216

219217
### 右值引用和左值引用
220218

0 commit comments

Comments
 (0)