Skip to content

Commit

Permalink
book: translate ch03
Browse files Browse the repository at this point in the history
see #12
  • Loading branch information
changkun committed Jul 20, 2019
1 parent 09537ae commit 6238d66
Show file tree
Hide file tree
Showing 15 changed files with 808 additions and 180 deletions.
605 changes: 605 additions & 0 deletions book/en-us/03-runtime.md

Large diffs are not rendered by default.

10 changes: 3 additions & 7 deletions book/en-us/toc.md
Original file line number Diff line number Diff line change
Expand Up @@ -42,17 +42,13 @@
- [**Chapter 03 Language Runtime Enhancements**](./03-runtime.md)
+ 3.1 Lambda expression
+ Basics
+ Value capture
+ Reference capture
+ Implicit capture
+ Expression capture
+ Generic lambda
+ Generics
+ 3.2 Function object wrapper
+ std::function
+ std::bind/std::placeholder
+ 3.3 rvalue reference
+ lvalue, rvalue, prvalue, xvalue
+ rvalue reference & lvalue reference
+ rvalue reference and lvalue reference
+ Move semantics
+ Perfect forwarding
- [**Chapter 04 Containers**](./04-containers.md)
Expand Down Expand Up @@ -87,7 +83,7 @@
+ 7.4 Condition Variable
+ 7.5 Atomic Operation and Memory Model
+ Atomic Operation
+ Concistency Model
+ Consistency Model
+ Memory Orders
- [**Chapter 08 File System**](./08-filesystem.md)
+ 8.1 Documents and links
Expand Down
4 changes: 2 additions & 2 deletions book/zh-cn/03-runtime.md
Original file line number Diff line number Diff line change
Expand Up @@ -317,8 +317,8 @@ void foo() {
}
```
由于 int& 不能引用 double 类型的参数,因此必须产生一个临时值来保存 s 的值,
从而当 increase() 修改这个临时值时,从而调用完成后 s 本身并没有被修改。
由于 `int&` 不能引用 `double` 类型的参数,因此必须产生一个临时值来保存 `s` 的值,
从而当 `increase()` 修改这个临时值时,从而调用完成后 `s` 本身并没有被修改。
第二个问题,为什么常量引用允许绑定到非左值?原因很简单,因为 Fortran 需要。
Expand Down
10 changes: 3 additions & 7 deletions book/zh-cn/toc.md
Original file line number Diff line number Diff line change
Expand Up @@ -41,15 +41,11 @@
- 强类型枚举
- [**第 3 章 语言运行期的强化**](./03-runtime.md)
+ 3.1 lambda 表达式
+ lambda 表达式基础
+ 值捕获
+ 引用捕获
+ 隐式捕获
+ 表达式捕获
+ 泛型 lambda
+ 基础
+ 泛型
+ 3.2 函数对象包装器
+ std::function
+ std::bind/std::placeholder
+ std::bindstd::placeholder
+ 3.3 右值引用
+ 左值、右值的纯右值、将亡值、右值
+ 右值引用和左值引用
Expand Down
55 changes: 0 additions & 55 deletions code/3/3.1.cpp

This file was deleted.

61 changes: 61 additions & 0 deletions code/3/3.1.lambda.basic.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
//
// 3.1.lambda.basic.cpp
// chapter 03 runtime enhancement
// modern c++ tutorial
//
// created by changkun at changkun.de
// https://github.com/changkun/modern-cpp-tutorial
//


#include <iostream>
#include <utility>

void lambda_value_capture() {
int value = 1;
auto copy_value = [value] {
return value;
};
value = 100;
auto stored_value = copy_value();
std::cout << "stored_value = " << stored_value << std::endl;
// At this moment, stored_value == 1, and value == 100.
// Because copy_value has copied when its was created.
}

void lambda_reference_capture() {
int value = 1;
auto copy_value = [&value] {
return value;
};
value = 100;
auto stored_value = copy_value();
std::cout << "stored_value = " << stored_value << std::endl;
// At this moment, stored_value == 100, value == 100.
// Because copy_value stores reference
}

void lambda_expression_capture() {
auto important = std::make_unique<int>(1);
auto add = [v1 = 1, v2 = std::move(important)](int x, int y) -> int {
return x+y+v1+(*v2);
};
std::cout << add(3,4) << std::endl;
}

void lambda_generic() {
auto generic = [](auto x, auto y) {
return x+y;
};

std::cout << generic(1, 2) << std::endl;
std::cout << generic(1.1, 2.2) << std::endl;
}

int main() {
lambda_value_capture();
lambda_reference_capture();
lambda_expression_capture();
lambda_generic();
return 0;
}
29 changes: 14 additions & 15 deletions code/3/3.2.cpp → code/3/3.2.function.wrap.cpp
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
//
// 3.2.cpp
// 3.2.function.wrap.cpp
// chapter 03 runtime enhancement
// modern c++ tutorial
//
// created by changkun at changkun.de
// https://github.com/changkun/modern-cpp-tutorial
//
// std::function std::bind

#include <functional>
#include <iostream>

using foo = void(int); // 定义函数指针
using foo = void(int); // function pointer
void functional(foo f) {
f(1);
}
Expand All @@ -24,29 +24,28 @@ int foo3(int a, int b, int c) {
}

int main() {

auto f = [](int value) {
std::cout << value << std::endl;
};
functional(f); // 函数指针调用
f(1); // lambda 表达式调用
// std::function 包装了一个返回值为 int, 参数为 int 的函数
functional(f); // call by function pointer
f(1); // call by lambda expression

// std::function wraps a function that take int paremeter and returns int value
std::function<int(int)> func = foo2;

int important = 10;
std::function<int(int)> func2 = [&](int value) -> int {
return 1+value+important;
};
std::cout << func(10) << std::endl;
std::cout << func2(10) << std::endl;

// 将参数1,2绑定到函数 foo 上,但是使用 std::placeholders::_1 来对第一个参数进行占位

// bind parameter 1, 2 on function foo, and use std::placeholders::_1 as placeholder
// for the first parameter.
auto bindFoo = std::bind(foo3, std::placeholders::_1, 1,2);
// 这时调用 bindFoo 时,只需要提供第一个参数即可
// when call bindFoo, we only need one param left
bindFoo(1);



return 0;
}
39 changes: 0 additions & 39 deletions code/3/3.3.cpp

This file was deleted.

38 changes: 38 additions & 0 deletions code/3/3.3.rvalue.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
//
// 3.3.rvalue.cpp
// modern c++ tutorial
//
// created by changkun at changkun.de
// https://github.com/changkun/modern-cpp-tutorial
//


#include <iostream>
#include <string>

void reference(std::string& str) {
std::cout << "lvalue" << std::endl;
}
void reference(std::string&& str) {
std::cout << "rvalue" << std::endl;
}

int main()
{
std::string lv1 = "string,"; // lv1 is a lvalue
// std::string&& r1 = s1; // illegal, rvalue can't ref to lvalue
std::string&& rv1 = std::move(lv1); // legal, std::move can convert lvalue to rvalue
std::cout << rv1 << std::endl; // string,

const std::string& lv2 = lv1 + lv1; // legal, const lvalue reference can extend temp variable's lifecycle
// lv2 += "Test"; // illegal, const ref can't be modified
std::cout << lv2 << std::endl; // string,string

std::string&& rv2 = lv1 + lv2; // legal, rvalue ref extend lifecycle
rv2 += "string"; // legal, non-const reference can be modified
std::cout << rv2 << std::endl; // string,string,string,

reference(rv2); // output: lvalue

return 0;
}
16 changes: 16 additions & 0 deletions code/3/3.4.historical.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
//
// 3.4.historical.cpp
// modern c++ tutorial
//
// created by changkun at changkun.de
// https://github.com/changkun/modern-cpp-tutorial
//

#include <iostream>

int main() {
// int &a = std::move(1); // illegal, non-const lvalue reference cannot ref rvalue
const int &b = std::move(1); // legal, const lvalue reference can

std::cout << b << std::endl;
}
19 changes: 9 additions & 10 deletions code/3/3.4.cpp → code/3/3.5.move.semantics.cpp
Original file line number Diff line number Diff line change
@@ -1,36 +1,35 @@
//
// 3.4.cpp
// 3.5.move.semantics.cpp
// modern c++ tutorial
//
// created by changkun at changkun.de
// https://github.com/changkun/modern-cpp-tutorial
//
// 移动语义

#include <iostream>
class A {
public:
int *pointer;
A():pointer(new int(1)) {
std::cout << "构造" << pointer << std::endl;
std::cout << "construct" << pointer << std::endl;
}
A(A& a):pointer(new int(*a.pointer)) {
std::cout << "拷贝" << pointer << std::endl;
} // 无意义的对象拷贝
std::cout << "copy" << pointer << std::endl;
} // meaningless object copy
A(A&& a):pointer(a.pointer) {
a.pointer = nullptr;
std::cout << "移动" << pointer << std::endl;
std::cout << "move" << pointer << std::endl;
}
~A(){
std::cout << "析构" << pointer << std::endl;
std::cout << "destruct" << pointer << std::endl;
delete pointer;
}
};
// 防止编译器优化
// avoid compiler optimization
A return_rvalue(bool test) {
A a,b;
if(test) return a; // 等价于 static_cast<A&&>(a);
else return b; // 等价于 static_cast<A&&>(b);
if(test) return a; // equal to static_cast<A&&>(a);
else return b; // equal to static_cast<A&&>(b);
}
int main() {
A obj = return_rvalue(false);
Expand Down
Loading

0 comments on commit 6238d66

Please sign in to comment.