473,382 Members | 1,736 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,382 software developers and data experts.

templates def/decl/mechanism


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
3 1386
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
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
>> 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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

9
by: Anthony Heading | last post by:
Hi all, I've often found myself wanting to write code like the example here. Since both MSVC and gcc both reject it, I suspect it is indeed illegal. gcc: no type named `Name' in `class...
1
by: Vince C. | last post by:
Hi all, I've created XML documents that are described with a schema. I'm using those documents to create web pages. All my web pages contain a fixed header and a variable document part. The...
11
by: Peter Oliphant | last post by:
Is there any plan to support templates with managed code in the (near) future? For instance, VS.NET 2005... : )
4
by: Robert Frunzke | last post by:
Hello, I need to implement a custom allocator to speed up the allocation of a specific class in my project and instead of hardwiring it, I would "templatize"(?) it. The allocator should have...
25
by: Ted | last post by:
I'm putting the posts that follow here (hopefully they will follow here!) because they were rejected in comp.lang.c++.moderated. It behooves anyone reading them to first read the the thread of the...
28
by: NewToCPP | last post by:
Hi, I am just trying to find out if there is any strong reason for not using Templates. When we use Templates it is going to replicate the code for different data types, thus increasing the...
104
by: JohnQ | last post by:
Well apparently not since one can step thru template code with a debugger. But if I was willing to make the concession on debugging, templates would be strictly a precompiler thing? I have a...
7
by: veganders | last post by:
When making a general matrix class I came across the following situation that I don't have an explanation for. For some friend functions I have to declare them in advance while other works anyway....
26
by: puzzlecracker | last post by:
Team, C++ has been around since 1986, why templates are still regarded is a new feature by most compiler vendors and not fully supported (for example export feature). Look at other popular...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.