By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
454,497 Members | 2,010 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 454,497 IT Pros & Developers. It's quick & easy.

templates def/decl/mechanism

P: n/a

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

Nov 22 '05 #1
Share this Question
Share on Google+
3 Replies


P: n/a
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
Nov 22 '05 #2

P: n/a
On Mon, 21 Nov 2005, John Harrison wrote:

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


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
Nov 23 '05 #3

P: n/a
>> 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).

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
Nov 23 '05 #4

This discussion thread is closed

Replies have been disabled for this discussion.