mirror of
https://github.com/changkun/modern-cpp-tutorial.git
synced 2025-12-17 04:34:40 +03:00
Print out the stored_value1 and stored_value2 for the lambda example. (#36)
* Fix compilation error for structured binding The header <tuple> is missing. After adding it, it compiles fine with clang in c++17 mode. * Print out the stored_value1 and stored_value2 for the lambda example. To make it eaiser for the readers to understand the difference between lambda capture by value and by reference.
This commit is contained in:
@@ -16,6 +16,7 @@ void learn_lambda_func_1() {
|
|||||||
};
|
};
|
||||||
value_1 = 100;
|
value_1 = 100;
|
||||||
auto stored_value_1 = copy_value_1();
|
auto stored_value_1 = copy_value_1();
|
||||||
|
std::cout << "stored_value_1=" << stored_value_1 << std::endl;
|
||||||
// 这时, stored_value_1 == 1, 而 value_1 == 100.
|
// 这时, stored_value_1 == 1, 而 value_1 == 100.
|
||||||
// 因为 copy_value_1 在创建时就保存了一份 value_1 的拷贝
|
// 因为 copy_value_1 在创建时就保存了一份 value_1 的拷贝
|
||||||
}
|
}
|
||||||
@@ -27,6 +28,7 @@ void learn_lambda_func_2() {
|
|||||||
};
|
};
|
||||||
value_2 = 100;
|
value_2 = 100;
|
||||||
auto stored_value_2 = copy_value_2();
|
auto stored_value_2 = copy_value_2();
|
||||||
|
std::cout << "stored_value_2=" << stored_value_2 << std::endl;
|
||||||
// 这时, stored_value_2 == 100, value_1 == 100.
|
// 这时, stored_value_2 == 100, value_1 == 100.
|
||||||
// 因为 copy_value_2 保存的是引用
|
// 因为 copy_value_2 保存的是引用
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user