material/c++/universal/STL/function.md
2025-01-10 22:29:51 +08:00

450 B
Raw Blame History

function 包装器

提供更加一致的接口,避免多次实例化

对于调用特征标(call signature)一样的函数使用function进行包装

std::function<double(int)> fun1;
  • 可接受任何函数指针函数对象lambda表达式
template <typename T>
void print(T f, int a) {
	std::cout << f(a) << std::endl;
}

double f1(int a) {
	return a/1.0;
}

std::function<double(int)> fun1=f1;
print(fun1, 10);