<>I use C++ and plan a library with classes that have about 100 template parameters. Of course, I worried that having n template parameters, if the user needs every combination, we have 2^n different classes, which is a kind of code explosion. However, a user would need to do 2^n instantiations for that.
My question is: What are the main technical issues with so many template parameters?
Notes:
- With technical, I am not interested in subjective answers concerning readability, design etc. I mean facts like
- runtime
- code size
- maximum number of templates allowed
Code sample:
// here we have 2, but I have 100 template parameters
template<typename T1, typename T2>
class Class
{
T1 x;
T2 y;
int add(T1 _x, T2 _y) { return _x+_y; } // 4 instanciations possible?
Class<T2, T1>* swap() { return new Class<T2, T1>(); } // always 2 instanciations?
};
The main consideration would be code size, and that can have an impact on performance. Each different template instantiation will require the generation of all of the member functions that are used (or all, if the user does manual template instantiation). There will be no code reuse among the different template instantiations.
Besides that, any element for which you provide a large number of arguments is hard to deal with. From the maintenance point of view, a declaration with 10 arguments is already hard to read. It will either extend multiple lines or else extend very wide in the line and it will be hard to determine by inspection that all arguments are in the correct position. Is that X the 7th or 8th argument? Yes, you can count them, but it becomes painful. If the number of arguments is 100, the problem is just exacerbated.
Why do you want all those to be template arguments? Without any more information you will not get other suggestions, but there are most probably other designs for the same problem that don’t require that level of complexity. Maybe the arguments need not be known at compile time (they can be passed to functions/constructors in the form of arrays/vectors for non-type arguments), or maybe they can be grouped (one type template argument holding a set of related typedefs)…