prepare for c++17

This commit is contained in:
Changkun Ou
2018-03-26 09:08:36 +02:00
parent 8a3eb8f271
commit 71025d8bc6
39 changed files with 28 additions and 11 deletions

52
code/3/3.1.cpp Normal file
View File

@@ -0,0 +1,52 @@
//
// 3.1.cpp
// c++1x tutorial
//
// created by changkun at shiyanlou.com
//
// lambda expression
#include <iostream>
#include <utility>
void learn_lambda_func_1() {
int value_1 = 1;
auto copy_value_1 = [value_1] {
return value_1;
};
value_1 = 100;
auto stored_value_1 = copy_value_1();
// 这时, stored_value_1 == 1, 而 value_1 == 100.
// 因为 copy_value_1 在创建时就保存了一份 value_1 的拷贝
}
void learn_lambda_func_2() {
int value_2 = 1;
auto copy_value_2 = [&value_2] {
return value_2;
};
value_2 = 100;
auto stored_value_2 = copy_value_2();
// 这时, stored_value_2 == 100, value_1 == 100.
// 因为 copy_value_2 保存的是引用
}
int main() {
learn_lambda_func_1();
learn_lambda_func_2();
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;
// 泛型 lambda
auto generic = [](auto x, auto y) {
return x+y;
};
generic(1, 2);
generic(1.1, 2.2);
return 0;
}

51
code/3/3.2.cpp Normal file
View File

@@ -0,0 +1,51 @@
//
// 3.2.cpp
// c++1x tutorial
//
// created by changkun at shiyanlou.com
//
// std::function std::bind
#include <functional>
#include <iostream>
using foo = void(int); // 定义函数指针
void functional(foo f) {
f(1);
}
int foo2(int para) {
return para;
}
int foo3(int a, int b, int c) {
return 0;
}
int main() {
auto f = [](int value) {
std::cout << value << std::endl;
};
functional(f); // 函数指针调用
f(1); // lambda 表达式调用
// std::function 包装了一个返回值为 int, 参数为 int 的函数
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 来对第一个参数进行占位
auto bindFoo = std::bind(foo3, std::placeholders::_1, 1,2);
// 这时调用 bindFoo 时,只需要提供第一个参数即可
bindFoo(1);
return 0;
}

38
code/3/3.3.cpp Normal file
View File

@@ -0,0 +1,38 @@
//
// 3.3.cpp
// c++1x tutorial
//
// created by changkun at shiyanlou.com
//
// 右值引用 rvalue reference
#include <iostream>
#include <string>
void reference(std::string& str) {
std::cout << "左值" << std::endl;
}
void reference(std::string&& str) {
std::cout << "右值" << std::endl;
}
int main()
{
std::string lv1 = "string,"; // lv1 是一个左值
// std::string&& r1 = s1; // 非法, 右值引用不能引用左值
std::string&& rv1 = std::move(lv1); // 合法, std::move可以将左值转移为右值
std::cout << rv1 << std::endl; // string,
const std::string& lv2 = lv1 + lv1; // 合法, 常量左值引用能够延长临时变量的申明周期
// lv2 += "Test"; // 非法, 引用的右值无法被修改
std::cout << lv2 << std::endl; // string,string
std::string&& rv2 = lv1 + lv2; // 合法, 右值引用延长临时对象声明周期
rv2 += "string"; // 合法, 非常量引用能够修改临时变量
std::cout << rv2 << std::endl; // string,string,string,
reference(rv2); // 输出左值
return 0;
}

31
code/3/3.4.cpp Normal file
View File

@@ -0,0 +1,31 @@
//
// 3.4.cpp
// c++1x tutorial
//
// created by changkun at shiyanlou.com
//
// 移动语义
#include <iostream>
class A {
public:
int *pointer;
A():pointer(new int(1)) { std::cout << "构造" << pointer << std::endl; }
A(A& a):pointer(new int(*a.pointer)) { std::cout << "拷贝" << pointer << std::endl; } // 无意义的对象拷贝
A(A&& a):pointer(a.pointer) { a.pointer = nullptr;std::cout << "移动" << pointer << std::endl; }
~A(){ std::cout << "析构" << pointer << std::endl; delete pointer; }
};
// 防止编译器优化
A return_rvalue(bool test) {
A a,b;
if(test) return a;
else return b;
}
int main() {
A obj = return_rvalue(false);
std::cout << "obj:" << std::endl;
std::cout << obj.pointer << std::endl;
std::cout << *obj.pointer << std::endl;
return 0;
}

32
code/3/3.5.cpp Normal file
View File

@@ -0,0 +1,32 @@
//
// 3.5.cpp
// c++1x tutorial
//
// created by changkun at shiyanlou.com
//
// 移动语义
#include <iostream> // std::cout
#include <utility> // std::move
#include <vector> // std::vector
#include <string> // std::string
int main() {
std::string str = "Hello world.";
std::vector<std::string> v;
// 将使用 push_back(const T&), 即产生拷贝行为
v.push_back(str);
// 将输出 "str: Hello world."
std::cout << "str: " << str << std::endl;
// 将使用 push_back(const T&&), 不会出现拷贝行为
// 而整个字符串会被移动到 vector 中,所以有时候 std::move 会用来减少拷贝出现的开销
// 这步操作后, str 中的值会变为空
v.push_back(std::move(str));
// 将输出 "str: "
std::cout << "str: " << str << std::endl;
return 0;
}

36
code/3/3.6.cpp Normal file
View File

@@ -0,0 +1,36 @@
//
// 3.6.cpp
// c++1x tutorial
//
// created by changkun at shiyanlou.com
//
// 完美转发
#include <iostream>
#include <utility>
void reference(int& v) {
std::cout << "左值引用" << std::endl;
}
void reference(int&& v) {
std::cout << "右值引用" << std::endl;
}
template <typename T>
void pass(T&& v) {
std::cout << "普通传参:";
reference(v);
std::cout << "std::move 传参:";
reference(std::move(v));
std::cout << "std::forward 传参:";
reference(std::forward<T>(v));
}
int main() {
std::cout << "传递右值:" << std::endl;
pass(1);
std::cout << "传递左值:" << std::endl;
int v = 1;
pass(v);
return 0;
}