473,563 Members | 2,897 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 2589
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...@ve rizon.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_declar ation

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

template< ... class_declarati on

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 objektorientier ter Datenverarbeitu ng
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
12509
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 <class T> class test{ test(void); ~test(void); friend bool operator== <> (const test<T> &p1, const test<T> &p2); }
5
2690
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: //**************************************************** // testClass.h //**************************************************** #ifndef TESTCLASS_H
5
2153
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 declaration (template or not) - .hxx files are template impl
2
3966
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 MyClass { // ... friend void MyFriendFunction (MyClass <T> * const ptrMyClass); // ...
1
1977
by: ranges22 | last post by:
****************************************************************** I am compiling a librarry which has a .h file containing th following: ****************************************************************** template<typename T> void from_string(const char Str, T &Obj); template<> void from_string(const char Str, long &); // template<>...
4
3315
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
774
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
2947
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
3769
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 void UseMap (const std::string &key, int value)
0
7665
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
0
7888
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
0
8106
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
0
7950
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
0
6255
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
1
5484
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 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 a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
5213
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
1
1200
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
924
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

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.