473,382 Members | 1,717 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,382 software developers and data experts.

Deleting a map of pointers with functors

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
Jul 22 '05 #1
1 1590
"Satish" <hs******@hotmail.com> wrote in message
news:c2**************************@posting.google.c om...
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
Jul 22 '05 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: Michael | last post by:
Ok, I understand the function pointers in C, but i'm a bit more confused in c++. Firstly, as i understand it, a static member function can be "function-pointed" to as a normal c function, but in...
9
by: Aguilar, James | last post by:
Hey guys. A new question: I want to use an STL libarary to hold a bunch of objects I create. Actually, it will hold references to the objects, but that's beside the point, for the most part. ...
3
by: Oliver Kowalke | last post by:
Hello, I've to use a function from c-library (third party) which expects a function pointer (void(*FunctionPointer)(int)). Instead of using a global function (with the signature of the function...
4
by: Ian Malone | last post by:
I have a class which looks like this: class reco { public: int height, width; double beta; double mu; simple_d_field estimate; simple_d_field auxiliary;
10
by: Anonymoose | last post by:
Hello, Hope someone can tell me how to do this - I'm an old 'C' hack so sometimes I get stuck in old-think...this is one of those times... I've got a bunch of methods in a class that I want to...
11
by: Protoman | last post by:
Which is more efficient for stuff like callbacks, selecting a funct from a list, and other stuff, function objects or function pointers? Thanks!!!
4
by: tryptik | last post by:
Hello all, I have a question about iterators. I have a container of functors that operate on an std::string. The functors go something like this: class Functor { std::string...
2
by: Jon Slaughter | last post by:
I'm trying to mess with functors and the way I want it to work is that when I create a functor it will automatically add itself to an array. The attached code demonstrates what I mean. The...
2
by: Eric Lilja | last post by:
As the topic says, I wanted to make a re-usable singleton class that could create pointers to objects with non-trivial constructors. I came up with this: #ifndef SINGLETON_HPP #define...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.