Velocity Reviews

Velocity Reviews (http://www.velocityreviews.com/forums/index.php)
-   C++ (http://www.velocityreviews.com/forums/f39-c.html)
-   -   template method instantiation (http://www.velocityreviews.com/forums/t276526-template-method-instantiation.html)

robc 08-08-2003 11:49 AM

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;
};

Jakob Bieling 08-08-2003 12:25 PM

Re: template method instantiation
 
"robc" <robcrompton@breathe.com> wrote in message
news:ffcb100d.0308080349.51733a49@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)




All times are GMT. The time now is 01:51 AM.

Powered by vBulletin®. Copyright ©2000 - 2014, vBulletin Solutions, Inc.
SEO by vBSEO ©2010, Crawlability, Inc.