I have a template class like below.
template<int S> class A { private: char string[S]; public: A() { for(int i =0; i<S; i++) { . . } } int MaxLength() { return S; } };
If i instantiate the above class with different values of S, will the compiler create different instances of A() and MaxLenth() function? Or will it create one instance and pass the S as some sort of argument?
How will it behave if i move the definition of A and Maxlength to a different cpp file.
The template will be instantiated for each different values of S.
If you move the method implementations to a different file, you’ll need to #include that file. (Boost for instance uses the
.ippconvention for such source files that need to be #included).If you want to minimise the amount of code that is generated with the template instantiation (and hence needs to be made available in the
.ippfile) you should try to factor it out by removing the dependency on S. So for example you could derive from a (private) base class which provides member functions with S as a parameter.