Skip to content

Commit 3abc907

Browse files
authored
book: fix incorrect string literal (changkun#102) and a typo (changkun#103)
1 parent f1c1e06 commit 3abc907

File tree

2 files changed

+34
-2
lines changed

2 files changed

+34
-2
lines changed

book/en-us/03-runtime.md

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -260,6 +260,22 @@ Temporary variables returned by non-references, temporary variables generated
260260
by operation expressions, original literals, and Lambda expressions
261261
are all pure rvalue values.
262262
263+
Note that a string literal became rvalue in a class, and remains an lvalue in other cases (e.g., in a function):
264+
265+
```cpp
266+
class Foo {
267+
const char*&& right = "this is a rvalue";
268+
public:
269+
void bar() {
270+
right = "still rvalue"; // the string literal is a rvalue
271+
}
272+
};
273+
274+
int main() {
275+
const char* const &left = "this is an lvalue"; // the string literal is an lvalue
276+
}
277+
```
278+
263279
**xvalue, expiring value** is the concept proposed by C++11 to introduce
264280
rvalue references (so in traditional C++, pure rvalue and rvalue are the same concept),
265281
a value that is destroyed but can be moved.
@@ -617,4 +633,4 @@ Lambda expression
617633
618634
## Licenses
619635
620-
<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).
636+
<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).

book/zh-cn/03-runtime.md

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -225,6 +225,22 @@ int main() {
225225
要么是求值结果相当于字面量或匿名临时对象,例如 `1+2`。非引用返回的临时变量、运算表达式产生的临时变量、
226226
原始字面量、Lambda 表达式都属于纯右值。
227227

228+
需要注意的是,字符串字面量只有在类中才是右值,当其位于普通函数中是左值。例如:
229+
230+
```cpp
231+
class Foo {
232+
const char*&& right = "this is a rvalue"; // 此处字符串字面量为右值
233+
public:
234+
void bar() {
235+
right = "still rvalue"; // 此处字符串字面量为右值
236+
}
237+
};
238+
239+
int main() {
240+
const char* const &left = "this is an lvalue"; // 此处字符串字面量为左值
241+
}
242+
```
243+
228244
**将亡值(xvalue, expiring value)**,是 C++11 为了引入右值引用而提出的概念(因此在传统 C++中,
229245
纯右值和右值是同一个概念),也就是即将被销毁、却能够被移动的值。
230246
@@ -252,7 +268,7 @@ std::vector<int> v = foo();
252268

253269
### 右值引用和左值引用
254270

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

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

0 commit comments

Comments
 (0)