Search code examples
c++performancecachingmetaprogrammingstd-function

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


I'm working on an Entity Component System library for learning purpose, and I'm trying to make major changes to my design.

I'm going to split the class System with one update call to several update functions. But these functions could be anything, function menber or not. So I ended with a lot of different std::function type.

For performance reason, I don't really want to use a tuple (or maybe it's really efficient, tell me). I'd like to use an std::array, but how can I put every kind of std::function together? Well in fact it's a struct which contain std::function, but because of that the struct get the same type problem.

And if the array could be generate at compile time (whih no arguments) it will be the best.

There is an illustration(not real code) of how I'd like it to work.

//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);

Anyway thanks


Solution

  • An array is a fixed length sequence of values of specified type. A tuple is a fixed length sequence of values of specified types. A struct with no methods is a fixed length sequence of values of specified types.

    These all have the same performance characteristics with regard to retrieving elements (i.e. constant), and moving / copying (i.e. sizeof(Thing) bytes).

    What "performance problems" are you imagining?

    See: http://en.cppreference.com/w/cpp/language/object