Consider the following code:
template<typename T, size_t S> struct MyArray {
int length;
T contents_[S];
MyArray() { length = S; }
T& operator[](const int index) {
assert(index >= 0 && index < length);
return contents_[index];
}
int Length() { return length; }
};
Fundamentally, there is no reason to create separate copies of the Length functions and subscript operator for each value of S. But I'm concerned that in fact there will be duplication of these functions for each different value of S, limiting the usefulness of this approach.
(In case you are curious why I'm not using std::vector, it's because this is an embedded application without any heap based memory allocation)