Patrick Guio wrote:
Dear all,
I would like to use template stream insertion operator for a template
class Foo.
The template class has one template parameter and the template
parameter is of type std::vector<"basic data types"> or
std::vector<std::vector<"basic data types"> >.
Insertion operators can be separated into two types: one for the one
dimension vector and one for the two dimension vector.
I have declared the following three templates declarations in the header
template<typename T>
std::ostream & operator<<(std::ostream &os,
const Foo<T> &f);
template<typename T>
std::ostream & operator<<(std::ostream &os,
const Foo<std::vector<T> > &f);
template<typename T>
std::ostream & operator<<(std::ostream &os,
const Foo<std::vector<std::vector<T> > > &f);
and the definition file contains definitions for only the two last
declaration as well as explicit instantiation declarations for the types
I want to support.
template<typename T>
std::ostream & operator<<(std::ostream &os,
const Foo<std::vector<T> > &f)
{....}
template
std::ostream & operator<<(std::ostream &os,
const Foo<std::vector<unsigned char> > &f);
...
template<typename T>
std::ostream & operator<<(std::ostream &os,
const Foo<std::vector<std::vector<T> > > &f)
{...}
template
std::ostream & operator<<(std::ostream &os,
const Foo<std::vector<std::vector<unsigned
char> > > &f);
...
The first template declaration does not have any definition but is used
for the template argument deduction algorithm (I think?)
Is there any other better way to define/declare the template stream
operator for such a template class?
Sincerely,
Patrick
With template functions there is no need to declare template functions
that you don't define. The only possible result of that is linker errors
(instead of compiler errors).
Secondly what you are calling the 'explicit instantiations' are nothing
of the sort, for instance
std::ostream & operator<<(std::ostream &os,
const Foo<std::vector<std::vector<unsigned char> > > &f);
is just a regular function. There's no need for the keyword template (in
I'm surprised that's not a syntax error).
I would say you are getting confused between specialization of class
templates, and overloads of function templates.
john