3 回答

TA貢獻1834條經驗 獲得超8個贊
您的代碼幾乎與此等效:
#include <iostream>
class unnamed1
{
int& n;
public:
unnamed1(int& N) : n(N) {}
/* OK. Your this is const but you don't modify the "n" reference,
but the value pointed by it. You wouldn't be able to modify a reference
anyway even if your operator() was mutable. When you assign a reference
it will always point to the same var.
*/
void operator()() const {n = 10;}
};
class unnamed2
{
int n;
public:
unnamed2(int N) : n(N) {}
/* OK. Your this pointer is not const (since your operator() is "mutable" instead of const).
So you can modify the "n" member. */
void operator()() {n = 20;}
};
class unnamed3
{
int n;
public:
unnamed3(int N) : n(N) {}
/* BAD. Your this is const so you can't modify the "n" member. */
void operator()() const {n = 10;}
};
int main()
{
int n;
unnamed1 u1(n); u1(); // OK
unnamed2 u2(n); u2(); // OK
//unnamed3 u3(n); u3(); // Error
std::cout << n << "\n"; // "10"
}
因此,您可以將lambda看作是使用operator()生成的類,除非您說它是可變的,否則該類默認為const。
您也可以將[]內部捕獲的所有變量(顯式或隱式)視為該類的成員:[=]對象的副本或[&]對象的引用。當您聲明lambda時就將它們初始化,就像有一個隱藏的構造函數一樣。

TA貢獻1799條經驗 獲得超8個贊
我給人的印象是,按值捕獲的全部目的是允許用戶更改臨時值-否則,使用按引用捕獲幾乎總是更好,不是嗎?
問題是,它“幾乎”嗎?常見的用例似乎是返回或傳遞lambda:
void registerCallback(std::function<void()> f) { /* ... */ }
void doSomething() {
std::string name = receiveName();
registerCallback([name]{ /* do something with name */ });
}
我認為這mutable不是“幾乎”的情況。我認為“按值捕獲”類似于“允許我在捕獲的實體死亡后使用其值”,而不是“允許我更改其副本”。但這也許可以爭論。
- 3 回答
- 0 關注
- 547 瀏覽
添加回答
舉報