Reputation: 11492
The implementation of a template class must be contained in the header file where it was defined. Should the implementation of such a class be done in-class or regular (like you would to it with every other class) but just in the header file?
The problem i have with the regular approach is that the implementation becomes very bloated since you need to put the template definition infront. I would like to know which is the most common way though.
Upvotes: 2
Views: 1219
Reputation: 75130
Probably the most common way is to write the class definition, then write the implementation in another file, then #include
the implementation file at the bottom of the header file and don't list it in the files to be compiled. That way they are in different files but the compiler is satisfied because the definition and declaration is in the same file after preprocessing.
Example:
// header.h
template<typename T>
struct A {
int dostuff();
};
#include "header.template"
// header.template (not header.cpp, to make it clear that this doesn't get compiled)
template<typename T>
int A::dostuff() {
// do stuff
}
After the preprocessor is done, the file looks like
template<typename T>
struct A {
int dostuff();
};
template<typename T>
int A::dostuff() {
// do stuff
}
Upvotes: 2