What could I use to make a function take N number of arguments, where N is not know at programming time but is fixed at compile time (it is template parameter, in fact)?
The function in question is an access function which lies in a performance critical path, so I'm looking for the least overhead possible.
What first comes in mind is std::initializer_list
and although cheap, as I've been told, it's still an unneccessary object to create and copy. What's more, it has a funky way to access its elements with initializer_list::begin[i]
(which is another object I don't care for) and does not constraint the number of arguments to N exactly, but that is a minor complaint.
Second, there are template parameter packs. Could those be a viable candidate? I'll have to employ recursion to access N values.
What I'm aiming for I tried to show in this pseudo-code:
template<int dim, class T> class linear_searchspace {
template<T...args> operator() (args) {
return std::tie(some_kinda_structure[args[0]], some_kinda_structure[args[1]], some_kinda_structure[args[3]]);
}
};
Hoow could I bring it in a recursive form that would actualy work?
CLARIFICATION: args
are supposed to be coordinates. Each coordinate is the index of a value in a dimension. So N coordinates get passed, N values will be returned. It is like accessing N vectors at the same time. I would like to add an offset to each argument which depends on the index of the argument, as it is stored an array with the the index of the offset corresponding to the index of the argument. The calculation will be simple arithmetic.
And what would be the appropriate return type? The structure this will access will most probably hold numeric values, tuples at most. Is a std::tuple
the best I can do or is it possible to craft something more performant?
Regarding the arguments, anything goes, even macros. I would be very glad to hear what tricks you came up with over the years.