template<class T, int size = 50>
class genClass {
     T storage[size];
     void menberFun() {
            if (someVar < size){......}

which uses an integer wariable size as a parameter to template to a declaration of genClass, which does not include size as a parameter to template and yet allows for flexibility of the value of the size. Consider a declaration of genClass's constructor. Is there any advantage of one version over another?

7 Years
Discussion Span
Last Post by StuXYZ

As I understand the question: Is there an advantage of using size as a template variable rather than as a parameter in the constructor.

There are probabily several reasons to prefer either approach. But consider what you CAN'T do with a template parameter relative to a constructor parameter.

consider that operator= has been declared for each class, then

genClass<int,50> A;
genClass<int,80> B;

// this is a compile time error [ unless explicityly allowed]

but this is at best a runtime error (if you have checking) and not if you don't. Or maybe you allow it/want to accept this.

genClass<int> A(50);
genClass<int> B(80);
A=B;       // No compiler error

If the intension is the first code version, then you have added better compiler error detection. Which is good, since a compiler error is almost always easier and better to get than a runtime error.

Other reasons include that compile time speed. FFT's can be sped up if the size is know at compile time.

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.