I'm trying to figure out if there is any benefit of creating a class template where a buffer can be statically allocated, useful for small buffers located on the stack and dynamically allocated buffers.
I want to combine these
Code:template<int tsize> class MyStaticClass { char buffer[tsize]; public: void Method1(); void Method2(); };
If we can default the size template<int tsize = 0> so that default is zero which means that the class should be dynamically allocated.Code:class MyDynamicClass { char *buffer; public: void Method1(); void Method2(); };
Now if the template parameter is zero the buffer variable will be a pointer instead of a vector.Code:template<int tsize = 0> class MyClass { char buffer[tsize]; public: void Method1(); void Method2(); }; template<> class MyClass<0> { char *buffer; public: void Method1(); void Method2(); };
My question is the compiler will insert methods for every size of the tsize or is it smart enough to make it general so that for any tsize > 0 will be generic?
Also when using dynamically allocated MyClass you still have to have brackets, MyClass<>. Is there a way to make the class so that the brackets aren't needed when it is a dynamic allocated class?
Should I separate the dynamic case and the static case? Is it better to have a separate class for the dynamic allocated case and static case?
The reason for the merge is that I want to more generic classes so that both cases originates for the same base class but maybe I'm going to far in this case.



LinkBack URL
About LinkBacks


