Templates should always be defined inside class body. This is the most preferred solution, because it's simple, clear and allows you to avoid a lot of unnecessary typing.
Consider following class:
template <CharEncoding Encoding>
class UnicodeConverter
{
protected:
template <typename Input_char_type, typename Output_char_type>
class InternalHelper
{
public:
void function_a()
{
//do something
}
};
//...
};
Now imagine, what would you have to write if you wanted to have a separate definition file:
template <CharEncoding Encoding>
template <typename Input_char_type, typename Output_char_type>
void UnicodeConverter<Encoding>::InternalHelper<Input_char_type, Output_char_type>::function_a()
{
//do something
}
That is definitely not pretty nor readable, wouldn't you agree?
There are some situations, where definition must separated (for example, two templates, that depend on each other), but in most cases: avoid it!
The rule of placing implementation in separate .cpp
files, generally does not apply to templates. C++ standard library also is mostly header-based, as its extensively uses templates for everything.
Also, note, that my sample code is a real sample, taken from real app (slightly modified). And imagine, that this is not the most complicated template I have in this project.