Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > template method instantiation

Reply
Thread Tools

template method instantiation

 
 
robc
Guest
Posts: n/a
 
      08-08-2003
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;
};
 
Reply With Quote
 
 
 
 
Jakob Bieling
Guest
Posts: n/a
 
      08-08-2003
"robc" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed) 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)


 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Explicit instantiation of non-template member of template class Noah Roberts C++ 6 02-02-2011 09:53 PM
Instantiation of template method in template class Ed C++ 1 08-27-2008 06:41 AM
Explicit instantiation of STL vector demands explicit instantiation of all the templates it using internally. krunalbauskar@gmail.com C++ 1 12-25-2006 03:51 PM
explicit instantiation of template methods of template classes Thomas Maier-Komor C++ 6 05-19-2005 08:00 AM
Explicit template instantiation from template function doesn't compile? Fernando Cuenca C++ 4 09-06-2004 04:06 PM



Advertisments