I have a class myclass
that depends on some int
for, say, setting up the size of a vector member.
I can implement that as a non-type template parameter
template<int sz>
class myclass {
...
or
class myclass {
...
and then simply use sz
as a parameter in the constructor or other class methods.
Both would work in many cases. In some other cases (e.g., if myclass
refers to other templated classes or functions using sz
as a non-type template parameter), only the first option would work.
In cases where both can work, what are the possible reasons to prefer one or the other?
Besides my new coding in the future, this would also impact in what I do with some code I already have... whether to make efforts in "converting" one type of implementation to the other, or leave it as it is now.
I am not only asking about the differences (e.g., allocation at compile vs. runtime), but also how these differences may make one or the other option preferable.