forked from changkun/modern-cpp-tutorial
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Showing
15 changed files
with
808 additions
and
180 deletions.
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.