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.
source share