Q : std::同一数组内具有不同类型的函数

Q : std::function with different type inside the same array

本文关键字:同类型 函数 std 数组      更新时间:2023-10-16

我正在研究一个实体组件系统库以用于学习目的,我正在尝试对我的设计进行重大更改。

我将通过对多个update函数的一个update调用来拆分class System。但这些功能可以是任何东西,无论是否功能。所以我以许多不同的std::function类型结束。

出于性能原因,我真的不想使用tuple(或者它真的很有效,告诉我)。我想使用std::array,但是如何将各种std::function放在一起?好吧,实际上这是一个包含std::functionstruct,但正因为如此,struct得到相同类型的问题。

如果数组可以在编译时生成(没有参数),那将是最好的。

有一个插图(不是真正的代码)我希望它如何工作。

//Function to add a function in the list [[Call by User]]
addSystemBloc<Position, Speed>(function(ArgIDontKnow, Position, Speed), otherThings);
//Function to generate a structure (probably a graph) linked to the array
generateGraph();
//Inside the initialization of the System using the function we just add [[ Call by User ]]
addArgument(id, arg); //id is not really nice, and this guy know the function he wants to add an arg (with std::bind)
//Somwhere in the main loop [[ Call by Library ]]
invoke(func, Position, Speed);

无论如何,谢谢

数组

是指定类型的值的固定长度序列。元组是指定类型的值的固定长度序列。没有方法的结构是指定类型的值的固定长度序列。

它们在检索元素(即常量)和移动/复制(即大小(事物)字节)方面都具有相同的性能特征。

您在想象什么"性能问题"?

请参阅:http://en.cppreference.com/w/cpp/language/object