lambda表达式用法:
lambda声明方式:
- capture->return-type{body}
如何用模板类型的函数作为实参?
例如:
template<typename T>
struct isFunctor : std::false_type {
};
template<typename L, typename R, typename... Args>
struct isFunctor<R (L::*)(Args...)> : std::true_type {
};
template<typename L>
struct isLambda : isFunctor<decltype(&L::operator())> {
};
template<typename L>
std::enable_if<isLambda<L>::value> check(L lambda) {
cout << " lambda(1, 2) = " << lambda(1, 2) << lambda(1, 2);
}
int main() {
auto fun = [&](int a, int b) {
a++;
b++;
cout << a <<"."<< b << endl;
return pow(a, b);
};
check(fun);
return 0;
}
//输出为: 2.3
// 2.3
// lambda(1, 2) = 88