I am new to TMP world and I need some help regarding the use of vectors in boost mpl or fusion.
So here is the situation:
I have an API for asynchronous function calls in a multithreaded environment which is implemented as a runtime library say: async_call(function_ptr, arg1, arg2, ... argN); Functions have variable number of arguments.
So the code of an application would look like the following:
void funcA(int a){
// code
}
void funcB(int a, double b, char C){
// code
}
int main(){
int a = 1;
double b = 2.0;
char c = 'C';
async_call(funcA, a);
async_call(funcB, a,b,c);
}
The library stores the arguments values in some internal data structures and executes the functions at some time in the future determined by the library. In order to implement some optimization, I need to know all the possible different number of arguments for the specific application and populate a const array containing the number of arguments: So during compilation in need to populate something like:
const int ArgsNumber[] = {1,3};
I have implemented async_call as a variadic template function and internally I count the number of arguments (count_args<...> of each submitted function using some template metaprogramming
template<typename Tr, typename ...Tn>
void async_call(Tr (*func)(Tn...), Tn... args) {
// ...
int args_num = count_args<Tn...>::value;
// ...
}
And here is the question: Can I populate a global MPL or FUSION vector with the count_args<...> results and then convert it to a const array?
I have seen some code proposing the boost preprocessor to generate the const array from an MPL vector like this:
#define MACRO(z, i, data) \
mpl::at_c<data,i>::value
static const data[] = { BOOST_PP_ENUM(N, MACRO, argsTable) };
So I declare an mpl vector globally:
typedef mpl::vector_c<int> argsTable;
and try to push_back from the async_call function (which is called from main) like this:
typedef typename mpl::push_back<argsTable,mpl::int_<count_args<Tn...>::value>>::type xyz;
However the vector does not get updated since I have to somehow "update" the argsTable with the new Sequence returned by push_back! Can I do this? Is MPL sufficient or do I need fusion?
Any other suggestions for a solution are more than welcome!