Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Too many template parameters in C++?

Tags:

c++

templates

<>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?
};
like image 825
Johannes Avatar asked Oct 22 '25 07:10

Johannes


2 Answers

Actually templated class types are determined at compile time. This does not mean that you have 2^n different classes but instead you have only n classes. At compile time the appropriate types are replaced and the class members/functions are only of the type that you are using them with.

E.g.

template <class Type1, class Type2>
class A
{
private:
    Type1 member_x;
public:
    Type2 GetTypeValue(Type1, Type2);
};

When instantiated like this:

A<int, string> *x = new A<int, string>();

Is being compiled only as a class with an integer type member and a string type member. Same stands for functions, etc.

Update: With the updated example below you would still have only one instance of the function and it would be a string returning function taking parameters int and string.

like image 170
Konstantin Dinev Avatar answered Oct 23 '25 20:10

Konstantin Dinev


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)...

like image 40
David Rodríguez - dribeas Avatar answered Oct 23 '25 19:10

David Rodríguez - dribeas