Deleting a map of pointers with functors

Discussion in 'C++' started by Satish, Sep 10, 2004.

  1. Satish

    Satish Guest

    Hi,

    I am trying to create a helper function that will delete all the
    pointers in a map. The function takes a bool parameter that tells if
    the key is of pointer type or if the value is of pointer type. The
    boolean value is used to call the appropriate functor that deletes the
    pointer.

    I have reduced the code to the minimum for demonstrating the problem.
    Can anyone point out what's wrong with this code. I am getting
    compiler errors both on g++ and MSVC.

    #include <iostream>
    #include <map>
    #include <algorithm>

    struct DeleteMapKey
    {
    template<typename MapValueType>
    void operator()(MapValueType v){ delete v.second; }
    };

    struct DeleteMapValue
    {
    template<typename MapValueType>
    void operator()(MapValueType v) { delete v.first; }
    };

    template<typename MapContType>
    void DeleteMap(MapContType& mapCont, bool deleteKey)
    {
    if(deleteKey == true)
    std::for_each(mapCont.begin(), mapCont.end(), DeleteMapKey());
    else
    std::for_each(mapCont.begin(), mapCont.end(), DeleteMapValue());
    }

    int main()
    {
    using namespace std;

    std::map<int, int*> valueMap;
    keyMap[new int(1)] = 1;
    DeleteMap(keyMap, true);

    std::map<int*, int> keyMap;
    valueMap[1] = new int(1);
    DeleteMap(valueMap, false);

    return 0;
    }

    Any help is appreciated. Thanks.
    Satish
     
    Satish, Sep 10, 2004
    #1
    1. Advertising

  2. Satish

    David Hilsee Guest

    "Satish" <> wrote in message
    news:...
    > Hi,
    >
    > I am trying to create a helper function that will delete all the
    > pointers in a map. The function takes a bool parameter that tells if
    > the key is of pointer type or if the value is of pointer type. The
    > boolean value is used to call the appropriate functor that deletes the
    > pointer.
    >
    > I have reduced the code to the minimum for demonstrating the problem.
    > Can anyone point out what's wrong with this code. I am getting
    > compiler errors both on g++ and MSVC.
    >
    > #include <iostream>
    > #include <map>
    > #include <algorithm>
    >
    > struct DeleteMapKey
    > {
    > template<typename MapValueType>
    > void operator()(MapValueType v){ delete v.second; }
    > };
    >
    > struct DeleteMapValue
    > {
    > template<typename MapValueType>
    > void operator()(MapValueType v) { delete v.first; }
    > };
    >
    > template<typename MapContType>
    > void DeleteMap(MapContType& mapCont, bool deleteKey)
    > {
    > if(deleteKey == true)
    > std::for_each(mapCont.begin(), mapCont.end(), DeleteMapKey());
    > else
    > std::for_each(mapCont.begin(), mapCont.end(), DeleteMapValue());
    > }
    >
    > int main()
    > {
    > using namespace std;
    >
    > std::map<int, int*> valueMap;
    > keyMap[new int(1)] = 1;
    > DeleteMap(keyMap, true);
    >
    > std::map<int*, int> keyMap;
    > valueMap[1] = new int(1);
    > DeleteMap(valueMap, false);
    >
    > return 0;
    > }


    The code that you have provided does not compile. However, I can tell you
    that you must change the DeleteMap function. If you ever write template
    code that looks like this

    template <class T>
    void foo( bool b ) {
    if ( b ) {
    // code that doesn't compile when T is of a certain type
    // but you're trying to avoid it by passing false
    }
    else {
    // code that doesn't compile when T is of another type
    // but you're trying to avoid it by passing true
    }
    }

    then you need to rewrite the code. Even if the code isn't going to be
    executed, it still must be compilable. In your case, you are going to have
    an if and an else that tries to pass delete an int. Consider breaking the
    code up into two functions: DeleteMapKey and DeleteMapValue.

    --
    David Hilsee
     
    David Hilsee, Sep 10, 2004
    #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. Paul MG
    Replies:
    2
    Views:
    413
    Dhruv
    Jul 3, 2003
  2. Xamalek
    Replies:
    7
    Views:
    708
  3. Harry Barker
    Replies:
    2
    Views:
    544
    Alf P. Steinbach
    Apr 19, 2006
  4. cerr

    pointers, pointers, pointers...

    cerr, Apr 7, 2011, in forum: C Programming
    Replies:
    12
    Views:
    731
  5. crea
    Replies:
    2
    Views:
    441
    Nobody
    Dec 28, 2012
Loading...

Share This Page