templates def/decl/mechanism

P

Patrick Guio

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::eek:stream & operator<<(std::eek:stream &os,
const Foo<T> &f);

template<typename T>
std::eek:stream & operator<<(std::eek:stream &os,
const Foo<std::vector<T> > &f);

template<typename T>
std::eek:stream & operator<<(std::eek:stream &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::eek:stream & operator<<(std::eek:stream &os,
const Foo<std::vector<T> > &f)
{....}
template
std::eek:stream & operator<<(std::eek:stream &os,
const Foo<std::vector<unsigned char> > &f);
....

template<typename T>
std::eek:stream & operator<<(std::eek:stream &os,
const Foo<std::vector<std::vector<T> > > &f)
{...}
template
std::eek:stream & operator<<(std::eek:stream &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
 
J

John Harrison

Patrick said:
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::eek:stream & operator<<(std::eek:stream &os,
const Foo<T> &f);

template<typename T>
std::eek:stream & operator<<(std::eek:stream &os,
const Foo<std::vector<T> > &f);

template<typename T>
std::eek:stream & operator<<(std::eek:stream &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::eek:stream & operator<<(std::eek:stream &os,
const Foo<std::vector<T> > &f)
{....}
template
std::eek:stream & operator<<(std::eek:stream &os,
const Foo<std::vector<unsigned char> > &f);
...

template<typename T>
std::eek:stream & operator<<(std::eek:stream &os,
const Foo<std::vector<std::vector<T> > > &f)
{...}
template
std::eek:stream & operator<<(std::eek:stream &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::eek:stream & operator<<(std::eek:stream &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
 
P

Patrick Guio

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).

I agree.
Secondly what you are calling the 'explicit instantiations' are nothing of
the sort, for instance

std::eek:stream & operator<<(std::eek:stream &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).

As you wrote it above, it is a regular function but I am refering to
explicit instantiation *declaration* as explained in
http://www.icce.rug.nl/documents/cplusplus/
in the section 18.3.1: Instantiation declarations

Patrick
 
J

John Harrison

Secondly what you are calling the 'explicit instantiations' are
As you wrote it above, it is a regular function but I am refering to
explicit instantiation *declaration* as explained in
http://www.icce.rug.nl/documents/cplusplus/
in the section 18.3.1: Instantiation declarations

Patrick

OK, I stand corrected. I was aware of explicit instantiations but
wrongly thought they only applied to class templates.

john
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,763
Messages
2,569,563
Members
45,039
Latest member
CasimiraVa

Latest Threads

Top