473,507 Members | 9,611 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

template method instantiation

hi,

I wrote a container template similar to that below, assuming that as
long as I never called destroy() for a non-pointer instantiation the
compiler would not attempt to generate that method and I would not get
a compile error. e.g:
ThingMap<int*> intPMap;
intPMap.destroy(); //ok for pointer type

ThingMap<int> intMap;
intPMap.clear(); //ok because destroy() not called so not
instantiated
// so no attempt to compile delete of int

Indeed, microsoft visual C++, Sun and HP compilers agreed with me and
compiled this OK. But IBM xlC gives a compile error: the "delete"
operator is not allowed for type "int".
So, am I wrong or is xlC wrong? I can work around this easily enough
but am interested to know.

thanks,

rob

#include <map>

template <class T> class ThingMap
{
public:
..
(other methods....)
..
void clear(){ theMap.clear(); }
void destroy()
{
for(stl::map<T>::iterator it=theMap.begin();
it!=theMap.end();++it)
delete it->second;
clear();
}
private:
stl::map<T> theMap;
};
Jul 19 '05 #1
1 2196
"robc" <ro*********@breathe.com> wrote in message
news:ff**************************@posting.google.c om...
hi,

I wrote a container template similar to that below, assuming that as
long as I never called destroy() for a non-pointer instantiation the
compiler would not attempt to generate that method and I would not get
a compile error. e.g:
ThingMap<int*> intPMap;
intPMap.destroy(); //ok for pointer type

ThingMap<int> intMap;
intPMap.clear(); //ok because destroy() not called so not
instantiated
// so no attempt to compile delete of int

Indeed, microsoft visual C++, Sun and HP compilers agreed with me and
compiled this OK. But IBM xlC gives a compile error: the "delete"
operator is not allowed for type "int".
So, am I wrong or is xlC wrong? I can work around this easily enough
but am interested to know.

No, you are right. If a member function of a template class is never
used, it is not instantiated.

hth
--
jb

(replace y with x if you want to reply by e-mail)
Jul 19 '05 #2

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

Similar topics

6
2005
by: Dave | last post by:
Hello all, Consider this function template definition: template<typename T> void foo(T) {} If foo is never called, this template will never be instantiated. Now consider this explicit...
7
4933
by: Andrew Ward | last post by:
Hi All, Considering the following code: struct A; struct B { std::list<A> l; };
7
2463
by: Drew McCormack | last post by:
I have a C++ template class which contains a static variable whose construction registers the class with a map. Something like this: template <typename T> class M { static Registrar<M>...
4
1637
by: Dave | last post by:
Hello all, Consider this template: template <typename T> void foo(T bar) {...} Here are three ways to instantiate this: 1.
6
1999
by: Thomas Maier-Komor | last post by:
Hi everybody, I am a little bit confused with the syntax of explicit instantiation, and I am not sure that it is possible to do what I want to do. Maybe someone has an idea. Consider a...
12
2597
by: mlimber | last post by:
This is a repost (with slight modifications) from comp.lang.c++.moderated in an effort to get some response. I am using Loki's Factory as presented in _Modern C++ Design_ for message passing in...
2
1995
by: Rudy Ray Moore | last post by:
Whenever I get any error with Vc++7.1/.net/2003, it is followed by huge ammounts of "template assistance" error messaging referencing template code (MTL) that has nothing to do with the error. ...
5
1662
by: dilip ranganathan | last post by:
Hi I have taken the liberty to cross-post this. It appeared on c.l.c++.m but the ICE is regarding VS.NET 7.1 C++ compiler. post follows: ==============================================...
2
1669
by: Andy Champ | last post by:
We have a class with something that, simplified, looks like this: template <typename Tclass foo { T beyondAllReaching; // In VC2005, this works OK template <typename Ufriend void...
1
2358
by: Ed | last post by:
Hi, guys, I declare a template method in one template class in one library. Compiling is OK, but link is not OK. Header File is: <code> template <typename P = float> class TESTLIB_API Linear...
0
7110
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
7314
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
7372
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...
0
7482
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...
0
4702
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...
0
3191
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
3179
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1540
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
0
411
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...

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.