Too many template options in C ++?

<> I use C ++ and plan a library with classes that have about 100 template parameters. Of course, I was worried that having n template parameters, if the user needs each combination, we have 2 ^ n different classes, which is a kind of code explosion. However, for this the user will need to execute 2 ^ n instances.

My question is: what are the main technical problems with so many template options?

Notes:

  • From the technical side, I am not interested in subjective answers regarding readability, design, etc. I mean facts like
    • at runtime
    • code size
    • Maximum number of patterns allowed

Code example:

// 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? }; 
+4
source share
2 answers

The focus will be on code size, and this can affect performance. For each specific instance of the template, the generation of all used member functions (or all if the user manually creates an instance of the template) is required. There will be no code reuse between different template instances.

In addition, any element for which you provide a large number of arguments is difficult to handle. In terms of service, a 10-argument ad is already hard to read. It will either expand several lines, or expand very wide in a line, and it will be difficult to determine by checking that all arguments are in the correct position. Is this X the 7th or 8th argument? Yes, you can count them, but it becomes painful. If the number of arguments is 100, the problem just gets worse.

Why do you want all these to be template arguments? Without additional information you will not receive other offers, but there are most likely other projects for the same problem that do not require such a level of complexity. Perhaps the arguments should not be known at compile time (they can be passed to functions / constructors as arrays / vectors for non-type arguments), or maybe they can be grouped (one type template argument contains a set of related typedefs) .. .

+3
source

In fact, template class types are defined at compile time. This does not mean that you have 2 ^ n different classes, but instead you only have n classes. At compile time, the corresponding types are replaced, and the members / functions of the class are replaced only with the type with which you use them.

eg.

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

When creating an instance:

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

It compiles only as a class with an integer type member and a string type member. The same means functions, etc.

Update: In the updated example below, you will still only have one instance of the function, and it will be a string return function with int and string parameters.

+4
source

Source: https://habr.com/ru/post/1436593/


All Articles