小编Roh*_*iya的帖子

如何在C ++中的lambda函数中传递相同名称的局部变量和参数(使用此关键字)?

#include <iostream>
#include <functional>
int main(){
    int a = 10; 
    std::function<int(int)> functionPointer = [a](int a)-> int{ return a + a + 100 ; };
    int returnValue = functionPointer(50);
    std::cout<<returnValue<<endl;
}
Run Code Online (Sandbox Code Playgroud)

我原本期望10 + 50 + 100 = 160,但是输出是10 + 10 + 100 =120。我可以做些改变以获得160,同时保持变量名称不变吗?

c++ lambda functional-programming inline-functions c++11

1
推荐指数
1
解决办法
96
查看次数