Deleting a map of pointers with functors

S

Satish

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
 
D

David Hilsee

Satish said:
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.
 

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,733
Messages
2,569,439
Members
44,829
Latest member
PIXThurman

Latest Threads

Top