Of course this works, but then I have to update my template header
file every time I instantiate a new template.
This is not very smart. Is there no better way ?
No, implicit instantation automatically creates the required
instantiations - this is why putting template definitions in the
header is the norm, since it requires no special maintenance. It does
however mean that code that uses the template is dependent on its
implementation.
But there is another way. Read down.
Make the above a non-template. You aren't going to be able to deduce T
in a call anyway. Consider this:
struct Foo
{
typedef int row;
};
struct Bar
{
typedef int row;
};
otl_stream << 5; //calls operator<< <Foo> or <Bar>!?
You can't deduce template arguments from nested types. So change your
friend declaration to:
//non-template friend:
friend otl_stream& operator<<(otl_stream &output, const typename
T::row& row);
In update.cc:
otl_stream& operator<<(otl_stream &output, const int& row)
{
//definition
}
otl_stream& operator<<(otl_stream &output, const whatever& row)
{
//definition
}
etc. Obviously, you could use another template in the .cc file and
forward the definitions to that to save duplication.
Not with the template as is, since template argument deduction cannot
work on the template as you have currently written it. However, in the
normal case you do this:
In .h:
template class forward declaration
template function (needing friendship) forward declaration
template class definition, including friend (as you wrote it)
In .cpp
Explicit instantion of required versions.
Tom