Is it possible to generate arguments based on int type of a template ?
I would like to generate something like
template<int num>
void func(int g,...){}
if num=10 then I would like my function becomes void func(int g, int gg, ….., int gggggggggg);
I would love to know if this is feasible. Thank you.
Variadic function arguments a la
<cstdarg>are not typesafe, and they’re not a good idea in C++. If you have C++11, you can use variadic templates for much better results.Be that as it may, if you want to go with variadic function arguments, you have to tell the function which arguments there are and how big they are. Traditionally, you would pass that information in one of the arguments (like
printfdoes). If you wanted to, you could use the template parameter for that effect, but since you have to have at least one non-variadic argument anyway, there’s really no need. Most importantly, making the function a template will instantiate a different piece of code for everyN!So, to summarize: Don’t use variadic functions. If you have to use variadic functions, don’t use templates with them.