c++的匿名函数捕获
捕获方式 | 捕获内容 | 示例 |
---|---|---|
[=] | 所有外部变量的值(包括 this 指针) | [=]() { std::cout << value; } |
[&] | 所有外部变量的引用(包括 this 指针) | [&]() { value = 100; } |
[variable] | 指定变量的值 | [value]() { std::cout << value; } |
[&variable] | 指定变量的引用 | [&value]() { value = 100; } |
[this] | 当前对象的 this 指针 | [this]() { this->memberFunction(); } |
[this, variable] | this 指针和指定变量的值 | [this, value]() { std::cout << this->value; } |
[this, &variable] | this 指针和指定变量的引用 | [this, &value]() { value = 100; } |
[=, this] | 所有外部变量的值和 this 指针 | [=, this]() { std::cout << this->value; } |
[&, this] | 所有外部变量的引用和 this 指针 | [&, this]() { value = 100; } |
1. 捕获所有变量的值:[=]
- 捕获方式:捕获所有外部变量的值(包括
this
指针)。 - 用途:允许在 Lambda 表达式中访问所有外部变量的值。
- 示例:
int value = 42; auto lambda = [=]() {std::cout << value << std::endl; // 使用捕获的 value 变量 }; lambda();
2. 捕获所有变量的引用:[&]
- 捕获方式:捕获所有外部变量的引用。
- 用途:允许在 Lambda 表达式中访问所有外部变量的引用,因此可以修改外部变量的值。
- 示例:
int value = 42; auto lambda = [&]() {value = 100; // 修改外部变量 value 的值 }; lambda(); std::cout << value << std::endl; // 输出 100
3. 捕获特定变量的值:[variable]
- 捕获方式:捕获指定变量的值。
- 用途:只捕获需要的变量,避免捕获不必要的变量。
- 示例:
int value = 42; int anotherValue = 100; auto lambda = [value]() {std::cout << value << std::endl; // 使用捕获的 value 变量 }; lambda();
4. 捕获特定变量的引用:[&variable]
- 捕获方式:捕获指定变量的引用。
- 用途:只捕获需要的变量的引用,允许在 Lambda 表达式中修改这些变量。
- 示例:
int value = 42; int anotherValue = 100; auto lambda = [&value]() {value = 100; // 修改外部变量 value 的值 }; lambda(); std::cout << value << std::endl; // 输出 100
5. 混合捕获:[=, &variable]
或 [&, variable]
- 捕获方式:可以混合使用值捕获和引用捕获。
- 用途:根据需要灵活捕获变量的值或引用。
- 示例:
int value = 42; int anotherValue = 100; auto lambda = [=, &anotherValue]() {std::cout << value << std::endl; // 使用捕获的 value 变量anotherValue = 200; // 修改外部变量 anotherValue 的值 }; lambda(); std::cout << anotherValue << std::endl; // 输出 200
6. 捕获 this
指针:[this]
- 捕获方式:捕获当前对象的
this
指针。 - 用途:允许在 Lambda 表达式中访问当前对象的成员变量和成员函数。
- 示例:
class MyClass { public:void memberFunction() {int value = 42;auto lambda = [this]() {std::cout << this->value << std::endl; // 使用捕获的 this 指针};lambda();} private:int value = 42; };
7. 捕获 this
指针并捕获其他变量:[this, variable]
- 捕获方式:捕获
this
指针和其他变量。 - 用途:在 Lambda 表达式中同时访问当前对象的成员和外部变量。
- 示例:
class MyClass { public:void memberFunction() {int value = 42;auto lambda = [this, value]() {std::cout << this->value << std::endl; // 使用捕获的 this 指针std::cout << value << std::endl; // 使用捕获的 value 变量};lambda();} private:int value = 42; };
8. 捕获 this
指针并捕获其他变量的引用:[this, &variable]
- 捕获方式:捕获
this
指针和其他变量的引用。 - 用途:在 Lambda 表达式中同时访问当前对象的成员和外部变量的引用。
- 示例:
class MyClass { public:void memberFunction() {int value = 42;auto lambda = [this, &value]() {std::cout << this->value << std::endl; // 使用捕获的 this 指针value = 100; // 修改外部变量 value 的值};lambda();std::cout << value << std::endl; // 输出 100} private:int value = 42; };
9. 捕获所有变量的值并捕获 this
指针:[=, this]
- 捕获方式:捕获所有外部变量的值并捕获
this
指针。 - 用途:在 Lambda 表达式中同时访问所有外部变量的值和当前对象的成员。
- 示例:
class MyClass { public:void memberFunction() {int value = 42;auto lambda = [=, this]() {std::cout << this->value << std::endl; // 使用捕获的 this 指针std::cout << value << std::endl; // 使用捕获的 value 变量};lambda();} private:int value = 42; };
10. 捕获所有变量的引用并捕获 this
指针:[&, this]
- 捕获方式:捕获所有外部变量的引用并捕获
this
指针。 - 用途:在 Lambda 表达式中同时访问所有外部变量的引用和当前对象的成员。
- 示例:
class MyClass { public:void memberFunction() {int value = 42;auto lambda = [&, this]() {std::cout << this->value << std::endl; // 使用捕获的 this 指针value = 100; // 修改外部变量 value 的值};lambda();std::cout << value << std::endl; // 输出 100} private:int value = 42; };