template method instantiation

Discussion in 'C++' started by robc, Aug 8, 2003.

  1. robc

    robc Guest

    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;
    };
    robc, Aug 8, 2003
    #1
    1. Advertising

  2. "robc" <> wrote in message
    news:...
    > 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)
    Jakob Bieling, Aug 8, 2003
    #2
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Fernando Cuenca
    Replies:
    4
    Views:
    2,515
    Gianni Mariani
    Sep 6, 2004
  2. Thomas Maier-Komor
    Replies:
    6
    Views:
    603
    Thomas Maier-Komor
    May 19, 2005
  3. Replies:
    1
    Views:
    562
    Salt_Peter
    Dec 25, 2006
  4. Ed
    Replies:
    1
    Views:
    340
  5. Noah Roberts
    Replies:
    6
    Views:
    1,133
    Johannes Schaub (litb)
    Feb 2, 2011
Loading...

Share This Page