C++ 中的泛型函数持有者

Generic function holder in c++

本文关键字:函数 持有者 泛型 C++      更新时间:2023-10-16

所以我知道如何在C++中使用typedef

但是,如果我想拥有一个通用函数数组,我可以做到吗?

例如:

foo() { cout << "hello world" << endl; }

foo2(int a) { cout << "hello friend number " << a << endl; }

var_type[] function_holder = { foo, foo2 }
function_holder[0]();
function_holder[1](1);

只需使用std::function

std::function<void()> functions[] = { 
foo, 
std::bind( foo2, 1 )
};
for( auto f : functions ) f();

现场示例