C++ 使用 lambda 初始化变量
C++ initialize variable with lambda
#include <iostream>
using namespace std;
int main()
{
static bool temp([]{
cout <<"Hi ";
return false;});
cout <<"temp "<< temp;
return 0;
}
它不执行lambda。但是如果我们像这样单独声明 lambda:
#include <iostream>
using namespace std;
int main()
{
auto lambda = []{
cout <<"Hi ";
return false;};
static bool temp(lambda());
cout <<"temp "<< temp;
return 0;
}
它会执行它。我在这里错过了什么?
#include <iostream>
using namespace std;
int main()
{
static bool temp([]{
cout <<"Hi ";
return false;});
cout <<"temp "<< temp;
return 0;
}
它不执行lambda。但是如果我们像这样单独声明 lambda:
#include <iostream>
using namespace std;
int main()
{
auto lambda = []{
cout <<"Hi ";
return false;};
static bool temp(lambda());
cout <<"temp "<< temp;
return 0;
}
它会执行它。我在这里错过了什么?