template method instantiation

R

robc

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

Jakob Bieling

robc said:
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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,535
Members
45,007
Latest member
obedient dusk

Latest Threads

Top