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

template declaration

P: n/a
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
Share this Question
Share on Google+
2 Replies


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

P: n/a
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 discussion thread is closed

Replies have been disabled for this discussion.