473,387 Members | 1,785 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,387 software developers and data experts.

template declaration

Does a template class declaration, like

template <class T>

have to come immediately prior to the declaration of the function,
e.g.,
T do_something (T something)
{ . . . }

that uses it?

I have not found anywhere where this rule is written down, but
my compiler seems to work that way. Every other declaration seems to
be able to be reused later. If template worked this way, then one
declaration of the template class would suffice for numerous function
declarations.

My understanding of templates and compilers is not very deep.

Thanks, Alan

Jun 19 '07 #1
2 2571
Alan wrote:
Does a template class declaration, like

template <class T>
That's not a complete declaration.
have to come immediately prior to the declaration of the function,
e.g.,
T do_something (T something)
{ . . . }

that uses it?
What do you mean by "uses it"?
I have not found anywhere where this rule is written down, but
my compiler seems to work that way. Every other declaration seems to
be able to be reused later. If template worked this way, then one
declaration of the template class would suffice for numerous function
declarations.

My understanding of templates and compilers is not very deep.
The rule is: if a declaration is sufficient, use it. Otherwise you
will need a definition.

A template need to be defined before the compiler has to instantiate
it ("use it"), but in many other places you can get away with just
a declaration. For example, when you're writing a function template,
it's OK not to fully define a template. However, you better have the
definition of a particular template (with its particular template
arguments) at the time when the function template is being instantiated
because the class template will be instantiated at the same time (if
it hasn't been instantiated before).

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
Jun 19 '07 #2
On Jun 19, 3:36 pm, Alan <jalantho...@verizon.netwrote:
Does a template class declaration, like
template <class T>
That's not a template class declaration. Strictly speaking,
there is no such thing as a template class declaration. If you
mean a class template declaration, that would be:

template< ... class ClassTemplate ;
have to come immediately prior to the declaration of the function,
e.g.,
T do_something (T something)
{ . . . }
that uses it?
You seem to be confusing something, although I'm not sure what.
There's nothing in the above function which uses any template
declaration whatsoever.
I have not found anywhere where this rule is written down, but
my compiler seems to work that way.
Work what way? You can use a template class declaration
anywhere it is in scope:

template< typename T class X ;

// anything you want here...

X< int >
doSomething( X< int const& arg )
{ ... }

No problem.

What are you actually trying to do? If you want to declare (or
define) a function template, then you have to use the syntax for
a function template:

template< ... function_declaration

If you want to declare (or define) a class template, you use the
syntax for a class template:

template< ... class_declaration

More generally, if you want to declare (or define) a template,
you precede whatever you want to declare (or define) as a
template with the keyword template, followed by its arguments,
and optionally preceded by the keyword export. Do that, and
you're no longer declaring a class or a function or whatever,
you're declaring a template (which is not a class or a function
until it is instantiated).
Every other declaration seems to
be able to be reused later.
Every declaration can be "reused" later. Otherwise, there would
be no point in making it.
If template worked this way, then one
declaration of the template class would suffice for numerous function
declarations.
It does. Obviously: you use std::vector in an number of
functions, although there is only one declaration of it. The
same holds for every template.

--
James Kanze (GABI Software, from CAI) email:ja*********@gmail.com
Conseils en informatique orientée objet/
Beratung in objektorientierter Datenverarbeitung
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34

Jun 20 '07 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Christophe Barbe | last post by:
I am not clear about friend functions of a template class. GCC (3.3.2) wants me to add <> after the friend function names in the class declaration and VisualC++ doesn't like that. template...
5
by: Trevor Lango | last post by:
What is the appropriate syntax for placing a friend function that includes as one of it's parameters a pointer to the class object itself within the template class? I have the following: ...
5
by: Nomak | last post by:
Hello all, i have two template classes which needs each other. I tried to write some fwd decl / decl / impl in a good way but i can't get it to compile. Explanations: - .hh files are for...
2
by: Ruben Campos | last post by:
I have a problem with a template function that is declared as a friend of a template class. I'll first show the exact problem with source code: // MyClass.hpp template <typename T> class...
1
by: ranges22 | last post by:
****************************************************************** I am compiling a librarry which has a .h file containing th following:...
4
by: Joseph Turian | last post by:
Hi, What is the correct syntax to get the bar<T>::f<int, unsigned>() function to compile in the following fragment? Thanks, Joseph class foo {
4
by: fdmfdmfdm | last post by:
I have the following code: #include <iostream> #include <cstdlib> #include <cassert> using namespace std; template <class T> class Stack{ public: enum{DefaultStack = 10, EmptyStack = -1};
8
by: flopbucket | last post by:
Hi, I want to provide a specialization of a class for any type T that is a std::map. template<typename T> class Foo { // ... };
5
by: chgans | last post by:
Hi all, I'm having difficulties with some template static member, especially when this member is a template instance, for example: ---- template<typename T> class BaseT { public: static...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
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: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
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
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...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

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.