A nice question for you. I'd like to study C++ templates, and I've found something that sound strange. This is my simple code, it implements a matrix class:
template <typename T>
Matrix<T>::Matrix(unsigned nrows, unsigned ncols)
{
int i;
matrix = new T* [ncols];
for (i=0; i<ncols; i++)
matrix[i] = new T;
}
[b]Main (main.cpp)[b]
#include <iostream>
using namespace std;
#include "matrix.h"
int main(int argc, char **argv)
{
Matrix<int> x(10,20);
}
If I try compiling this stuff the compiler works well, but there's a linker error such
/tmp/ccBo2hkV.o: In function `main':
main.cpp:(.text+0x84): undefined reference to `Matrix<int>::Matrix(unsigned int, unsigned int)'
collect2: ld returned 1 exit status
Althrough this approach stinks, I've tried to include the '.cpp' file into main.cpp instead of including the '.h' file, and it works.
Can you explain me what's going on?
Name:
Anonymous2009-04-15 10:04
I'd like to study C++ templates, and I've found something that sound strange
Me too.
You cannot export template definitions between compilation units, only specific instantiations (except using the export keyword, which only the Comeau Sepples Compiler supports).
You could explicitly instantiate Matrix<int> in the main.cpp if you like, but you have to do it for each possible type of Matrix. This is one of the reasons why Sepples templates suck.
Explicit instantiation looks something like this: template class Matrix<int>;
My advice to you: after you finish studying Sepples templates, leave Sepples and never look back.
This is a pretty sucky story. Why on the earth did they do a so poor language implementation? That's ok, let's keep it as it is... we don't have no alternative, after all.
Anyway I still need to know how templates-like mechanism can be obtained.
>>15
Put the template definitions in the header file rather than having a separate compilation unit. It's how it has to be done.
Also, you might consider using a single-dimension array so that the entire matrix is one continuous block of memory. Might make it a bit easier to pass the stuff to SSE2 instructions. You weren't going to implement matrix manipulations without SSE2, were you?
Put the template definitions in the header file rather than having a separate compilation unit. It's how it has to be done.
Thanks for helping me, I'll try it now.
With regard to SSE optimization, don't worry about that: I'm just doing experiments, this is not going to become a great and widespread library! Thanks anyway.
When templates are declared, the compiler will not have to process the template’s definitions again and again; and no instantiations will be created on the basis of template declarations alone. Any actually required instantiation must then be available elsewhere (of course, this holds true for declarations in general). Unlike the situation we encounter with ordinary functions, which are usually stored in libraries, it is currently not possible to store templates in libraries (although the compiler may construct precompiled header files). Consequently, using template declarations puts a burden on the shoulders of the software engineer, who has to make sure that the required instantiations exist