Skip to content

Commit

Permalink
book: fix incorrect string literal (changkun#102) and a typo (changku…
Browse files Browse the repository at this point in the history
  • Loading branch information
cathaysia authored Jun 3, 2020
1 parent f1c1e06 commit 3abc907
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 2 deletions.
18 changes: 17 additions & 1 deletion book/en-us/03-runtime.md
Original file line number Diff line number Diff line change
Expand Up @@ -260,6 +260,22 @@ Temporary variables returned by non-references, temporary variables generated
by operation expressions, original literals, and Lambda expressions
are all pure rvalue values.
Note that a string literal became rvalue in a class, and remains an lvalue in other cases (e.g., in a function):
```cpp
class Foo {
const char*&& right = "this is a rvalue";
public:
void bar() {
right = "still rvalue"; // the string literal is a rvalue
}
};
int main() {
const char* const &left = "this is an lvalue"; // the string literal is an lvalue
}
```

**xvalue, expiring value** is the concept proposed by C++11 to introduce
rvalue references (so in traditional C++, pure rvalue and rvalue are the same concept),
a value that is destroyed but can be moved.
Expand Down Expand Up @@ -617,4 +633,4 @@ Lambda expression
## Licenses
<a rel="license" href="http://creativecommons.org/licenses/by-nc-nd/4.0/"><img alt="Creative Commons License" style="border-width:0" src="https://i.creativecommons.org/l/by-nc-nd/4.0/88x31.png" /></a><br />This work was written by [Ou Changkun](https://changkun.de) and licensed under a <a rel="license" href="http://creativecommons.org/licenses/by-nc-nd/4.0/">Creative Commons Attribution-NonCommercial-NoDerivatives 4.0 International License</a>. The code of this repository is open sourced under the [MIT license](../../LICENSE).
<a rel="license" href="http://creativecommons.org/licenses/by-nc-nd/4.0/"><img alt="Creative Commons License" style="border-width:0" src="https://i.creativecommons.org/l/by-nc-nd/4.0/88x31.png" /></a><br />This work was written by [Ou Changkun](https://changkun.de) and licensed under a <a rel="license" href="http://creativecommons.org/licenses/by-nc-nd/4.0/">Creative Commons Attribution-NonCommercial-NoDerivatives 4.0 International License</a>. The code of this repository is open sourced under the [MIT license](../../LICENSE).
18 changes: 17 additions & 1 deletion book/zh-cn/03-runtime.md
Original file line number Diff line number Diff line change
Expand Up @@ -225,6 +225,22 @@ int main() {
要么是求值结果相当于字面量或匿名临时对象,例如 `1+2`。非引用返回的临时变量、运算表达式产生的临时变量、
原始字面量、Lambda 表达式都属于纯右值。

需要注意的是,字符串字面量只有在类中才是右值,当其位于普通函数中是左值。例如:

```cpp
class Foo {
const char*&& right = "this is a rvalue"; // 此处字符串字面量为右值
public:
void bar() {
right = "still rvalue"; // 此处字符串字面量为右值
}
};

int main() {
const char* const &left = "this is an lvalue"; // 此处字符串字面量为左值
}
```
**将亡值(xvalue, expiring value)**,是 C++11 为了引入右值引用而提出的概念(因此在传统 C++中,
纯右值和右值是同一个概念),也就是即将被销毁、却能够被移动的值。
Expand Down Expand Up @@ -252,7 +268,7 @@ std::vector<int> v = foo();

### 右值引用和左值引用

需要拿到一个将亡值,就需要用到右值引用的申明`T &&`,其中 `T` 是类型。
要拿到一个将亡值,就需要用到右值引用`T &&`,其中 `T` 是类型。
右值引用的声明让这个临时值的生命周期得以延长、只要变量还活着,那么将亡值将继续存活。

C++11 提供了 `std::move` 这个方法将左值参数无条件的转换为右值,
Expand Down

0 comments on commit 3abc907

Please sign in to comment.