473,659 Members | 2,645 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 2210
"robc" <ro*********@br eathe.com> wrote in message
news:ff******** *************** ***@posting.goo gle.com...
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
2010
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 instantiation of foo:
7
4943
by: Andrew Ward | last post by:
Hi All, Considering the following code: struct A; struct B { std::list<A> l; };
7
2474
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> registrar; }; The constructor of Registrar does the registering when it is initialized.
4
1645
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
2007
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 template class: template <class ABC> struct CT : public ABC
12
2612
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 an embedded environment with multiple processors. I created a policy for classes, which, I had hoped, would automatically register the class with the appropriate factory: // In some header file... #include <cassert>
2
2011
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. This makes it difficult to spot errors. For example, F4 to "jump to next error" just jumps ot the next "template assistance" message. And since there are hundreds of them, this is quite obnoxious. Can I disable these things? Why is MSVC...
5
1670
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: ============================================== John Torjo wrote: >
2
1687
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 method(foo<Tl, foo<Ur); // But VC2008 requires this
1
2373
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 { public:
0
8341
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
1
8539
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8630
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 choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
7360
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 launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6181
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 instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5650
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 into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4176
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 last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4342
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
1982
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.