template declaration

A

Alan

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
 
V

Victor Bazarov

Alan said:
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
 
J

James Kanze

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

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

No members online now.

Forum statistics

Threads
473,774
Messages
2,569,596
Members
45,143
Latest member
DewittMill
Top