Non-inline function definitions
Of course, there are times when you’ll
want to have non-inline member function definitions. In this case, the
compiler needs to see the template declaration before the member
function definition. Here’s the example above, modified to show the non-inline
member definition:
//: C16:Array2.cpp
// Non-inline template definition
#include "../require.h"
template<class T>
class Array { enum { size = 100 }; T A[size];
public:
T& operator[](int index);
};
template<class T>
T& Array<T>::operator[](int index) { require(index >= 0 && index < size,
"Index out of range");
return A[index];
}
int main() { Array<float> fa;
fa[0] = 1.414;
} ///:~
Any reference to a template’s class name
must be accompanied by its template argument list, as in Array<T>::operator[].
You can imagine that internally, the class name is being decorated with the
arguments in the template argument list to produce a unique class name
identifier for each template instantiation.