#include<iostream>
using namespace std;
int main(){
auto u=[](int a,int b){ cout<<a<<' '<<b<<endl;return 0;}; //u是一个函数对象
u(1,2); //执行u
return 0;
}
等价于
#include<iostream>
using namespace std;
int main(){
auto u=[](int a,int b){ cout<<a<<' '<<b<<endl;return 0;}(1,2);
return 0;
}
细节:
1.第二种方式必须带返回值,否则会[Error] 'void u' has incomplete type。第一种方式不会出现这种情况。
2.在以第二种方式声明并使用u后,u不可以再次作为函数被调用,[Error] 'u' cannot be used as a function。而第一种方式声明的u可以多次调用。
(dev c++)