Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/153.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/neo4j/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ c+中的泛型函数保持器+;_C++_Function_Pointers_Generics_Typedef - Fatal编程技术网

C++ c+中的泛型函数保持器+;

C++ c+中的泛型函数保持器+;,c++,function,pointers,generics,typedef,C++,Function,Pointers,Generics,Typedef,所以我知道如何在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]();

所以我知道如何在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);
foo(){cout只需使用:

std::function[]={
福,
绑定(foo2,1)
};
对于(自动f:函数)f();

您可以创建一个指向相同类型函数的函数指针数组,但不能按值将函数放入数组。
std::any
std::variant
可能会有所帮助
std::function<void()> functions[] = { 
   foo, 
   std::bind( foo2, 1 )
};

for( auto f : functions ) f();